Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Parse][Android] How to suppress push notification from being displayed when app is running?

I need to handle with a push notification in two ways:

1) Receive and put a notification on status bar of my android client when app is in background;

2) Receive and handle with a notification without showing it on status bar when my app is in foreground;

For (1) was very simple, I put the and call PushService.setDefaultPushCallback(context, classObject); and the notification appears on status bar correctly.

My problem is with the (2):

  • I tried to create a custom BroadCastReceiver, but parse takes the notification before me and shows it on status bar;
  • I tried to turn off PushService.setDefaultPushCallback(context, classObject) on onStart method by setting null value for classObject, but when I did that, my receiver was never called and the notification didn´t appear;

Is there anything I could do to intercept notifications before parse or is there another thing I could do to solve my problem?

ps: I need to send the message from the server with "alert"

Tks,

like image 256
user2494863 Avatar asked Jun 17 '13 21:06

user2494863


2 Answers

If you use "alert" or "title" in your json data, the com.parse.PushService will intercept and display a standard notification.

Rather create your own BroadCastReceiver and send the title as e.g. "header" in json. You can then control in your onReceive handler when and what to display.

e.g.

public class MyBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "MyBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();
            String channel = intent.getExtras().getString("com.parse.Channel");
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            String title = "New alert!";
            if (json.has("header"))
                title = json.getString("header");
            generateNotification(context, getImg(), title);
        } catch (Exception e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }
    }

    public static void generateNotification(Context context, int icon, String message) {
        // Show the notification
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, SnapClientActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.vibrate = new long[] { 500, 500 };
        notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        notification.flags = 
            Notification.FLAG_AUTO_CANCEL | 
            Notification.FLAG_SHOW_LIGHTS;

        notificationManager.notify(0, notification);
    }
}
like image 189
Hannes Avatar answered Sep 28 '22 02:09

Hannes


I had the same problem. And after trying unsuccesfully other solutions, I tried amuch more simple solution... and it worked.

When using your custom ParsePushBroadcastReceiver, override the onPushReceiver method. And only call the super class method in case your app is not active.

public class ParsePushReceiver extends com.parse.ParsePushBroadcastReceiver{

    @Override
    protected void onPushReceive(Context context, Intent intent ) {
        // do your stuff here
        if( !MyApp.active )
            super.onPushReceive(context, intent );
    }
}
like image 33
Ignacio Roda Avatar answered Sep 28 '22 02:09

Ignacio Roda