Reputation: 57
I have some Parse code that receives push data and sets up a notification which works fine but the problem is the onPushOpen declaration. I want to open a specific activity in my app but onPushOpen seems to never get called or just not work?
This is my code:
public class NotificationReceiver extends ParsePushBroadcastReceiver {
private static final String TAG = "MYTAG";
@Override
public void onReceive(Context context, Intent intent) {
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Log.i(TAG, "Notification received");
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data") : "";
JSONObject jObject;
String alert = null;
String title = null;
try {
jObject = new JSONObject(message);
alert = (jObject.getString("alert"));
title = (jObject.getString("title"));
}
catch (JSONException e) {
e.printStackTrace();
}
Log.i(TAG,"alert is " + alert);
Log.i(TAG,"title is " + title);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alert)
.setContentText(title);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification.build());
Log.i(TAG, "Notification created");
}
@Override
protected void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "Notification clicked");
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
I can't seem to figure out why onPushOpen doesn't start my requested activity (MainActivity). Any help would be greatly appreciated.
Upvotes: 2
Views: 689
Reputation: 1055
i'm facing the same problem with the onPushOpen()
method, but if you just want to open your activity, there is a little work around. you can solve the problem by using a Pending Intent like this:
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alert)
.setContentText(title);
// create intent to start your activity
Intent activityIntent = new Intent(context, MainActivity.class);
// create pending intent and add activity intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
// Add as notification
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification.build());
so it is not necessary to call the onPushOpen()
method, because as soon as the notification gets clicked the pending intent starts your activity.
Upvotes: 1