9

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,

user2494863
  • 451
  • 7
  • 17

4 Answers4

28

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);
    }
}
Hannes
  • 296
  • 3
  • 2
  • 1
    hi,can you tell me where to call the this MyBroadcastReceiver – SAndroidD May 23 '14 at 11:53
  • @SAndroidD Study broadcastReceivers more. They aren't "called" in the way your thinking. They are only called when you send a message. and Hannes : +1 Nicely Done. Thanks so much. – Chad Bingham Jun 25 '14 at 19:05
  • onReceive is never called in my app...for my custom broadcast receiver – Boldijar Paul Apr 08 '15 at 14:15
  • @Hannes i m trying to send json from android but failed to recieve in target mobile device ? – Erum Jun 04 '15 at 17:07
  • @LoyalRayne i m trying to send json from android but failed to recieve in target mobile device ? failed to send to specific user by pasing object id https://groups.google.com/forum/#!topic/parse-developers/9NOpSxxB2Ro – Erum Jun 05 '15 at 08:17
7

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 );
    }
}
Ignacio Roda
  • 151
  • 1
  • 2
  • 2
    This is really an elegant solution, just in case anybody else is wondering you have to replace reciever name of with your custom class name here it would ParsePushReciever – king_below_my_lord Jul 30 '15 at 13:19
  • This should be the accepted answer. Everything else is needlessly complicated. – jjjjjjjj Apr 05 '17 at 01:24
1

Don't forget to register your custom BroadcastReceiver in your AndroidManifest.xml, as Parse.com's documentation states:

If your code lives in the com.example package and you want to register a receiver for the com.example.UPDATE_STATUS action, you can add the following XML to your AndroidManifest.xml file, immediately after the end of the ParseBroadcastReceiver block that you created earlier:

<receiver android:name="com.example.MyCustomReceiver" android:exported="false">
  <intent-filter>
    <action android:name="com.example.UPDATE_STATUS" />
  </intent-filter>
</receiver>

Your custom receiver will be called whenever a push notification is received with an action parameter of com.example.UPDATE_STATUS. For security purposes, the Parse SDK ensures that this intent can only be handled by receivers within your app. You should additionally make sure to set the android:exported attribute on your element to prevent other apps from sending pushes to your receiver.

Shmulik Klein
  • 3,754
  • 19
  • 34
0

Hannes is correct, but that solution will still generate a notification irregardless of whether or not the app is running.

To answer your full question, use Hannes' code in conjunction with this here: How can I tell if Android app is running in the foreground?

Only when the app isn't running will you call generateNotification().

Community
  • 1
  • 1