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):
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,
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);
}
}
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 );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With