Question

I'm having a little problem. I have an AlarmManager who sends an intent to a BroadcastReceiver, in the onReceive method of that class I push a notification to the StatusBar... This works like a charm, but I need to open an activity when the user taps on the notification, but there is some problem in my code an after googling and searching in SO I couldn't find an answer... so this is my code to push the notif:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(this.context, NewCommit.class);     
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent sender = PendingIntent.getBroadcast(this.context, 192839, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder noti = new NotificationCompat.Builder(this.context)
    .setSmallIcon(R.drawable.status_bar_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setContentIntent(sender);



noti.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);


NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(181818, noti.build());

Any idea?? It's driving me crazy!

Thanks!

Was it helpful?

Solution

I double checked your code with a working one. I assume that NewCommit is your activity.

Apart from explicitly setting the activity in the intent (something like Intent intent = new Intent(c, NewCommit.class);)

I think the main difference (and the problem) is that you are calling

PendingIntent.getBroadcast

instead of

PendingIntent.getActivity

The doc says:

"Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top