48

Below is my block of code which should open NotificationActivity when the notification is tapped on. But its not working.

private void setNotification(String notificationMessage) {
    Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.drawable.logo)
    .setContentTitle("My Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setContentText(notificationMessage).setAutoCancel(true);
    mBuilder.setSound(alarmSound);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}
buczek
  • 2,011
  • 7
  • 29
  • 40
prashantwosti
  • 980
  • 2
  • 7
  • 17

6 Answers6

89

try this:

private void setNotification(String notificationMessage) {

//**add this line**
int requestID = (int) System.currentTimeMillis();

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

//**add this line**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

//**edit this line to put requestID as requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}
Zaartha
  • 1,106
  • 9
  • 25
droidx
  • 2,172
  • 19
  • 26
  • 14
    Can you give a reason why this works... this is extremely vague. – Elliott Apr 10 '14 at 23:13
  • Strange indeed. I can verify that this solution works. Thanks droidx. – kha Sep 08 '14 at 13:07
  • 6
    Worked for me aswell. Just passing a requestCode different than zero did the job. I'm only adding the flag: intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); – voghDev Oct 14 '14 at 11:29
  • 1
    Thank you stackoverflow Gods for this! – Lara Ruffle Coles Mar 10 '16 at 22:40
  • Confirmed this helped me as well. I solved a similar problem with a Samsung Galaxy S3 running 4.4.2 by changing the second parameter (requestCode) to PendingIntent.getService from a 0 to a 1. – mbafford Mar 28 '16 at 20:24
  • Worked for me, check the lines below the **add this line** and compare it to the code block in the start post. Don't know why it works, but hey, it's android so that is nothing new. – Harmen May 03 '16 at 12:45
  • @droidx This single line saved my days `int requestID = (int) System.currentTimeMillis();`. Thank you – M D Jan 02 '17 at 15:03
  • notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); was the star! – Hanoch Moreno Apr 29 '20 at 08:06
13

You might be using a notification id equal to 0. There is known issue with notification id being 0. If you will use any other id, it should work.

Jeremy B
  • 911
  • 7
  • 20
pratsJ
  • 3,369
  • 3
  • 22
  • 41
  • i didn't understand, should it be 0 or not? You are saying 'must be 0' and 'any other id should work' ?? – Buddy Aug 08 '15 at 16:11
  • I was guessing that the problem is that he must be using '0' which is causing the issue. It should not be 0 but any other id – pratsJ Aug 08 '15 at 20:13
7

I added task builder and the below block code worked for me

Intent intent = new Intent(context, HomeActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Ravi Gadipudi
  • 1,446
  • 1
  • 17
  • 30
  • I'm doing the same and it works. But - what if I want to have two different actions? I.e. content action and button action. How to create second pending intent? – Micer Jan 14 '18 at 05:57
  • @Micer You can create them separately and assign them in the respective methods. – rahulrvp Jan 03 '20 at 10:55
3

Try checking your AndroidManifest.xml, double check if you are navigating to an activity with an intent-filter main action, launcher category

For example,

enter image description here

I couldn't make my pending intent to navigate to HomeActivity but it works when navigating to SplashScreen

Hope this helps.

Rick Royd Aban
  • 904
  • 6
  • 33
  • It helps, but I'm not sure how to solve this if you do in fact want to navigate to "HomeActivity". Should we add the activity to one of the filters? Or specify an action? Or add an action to the intent-filter? – Bouke Versteegh Apr 13 '21 at 13:01
  • Coincidentally, it turns out my "activity" wasn't listed in this list, because it was only a subview of another activity. This was the issue in the end. Thanks :-) – Bouke Versteegh Apr 13 '21 at 13:05
2

If the app is already running then you need to handle it in onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    //Handle intent here...
}
Zeero0
  • 2,602
  • 1
  • 21
  • 36
2

Added the requestid, but the intent was still not opening.

This is the solution that worked for me:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Setting those flags to clear the activities below the intent activity.

live-love
  • 48,840
  • 22
  • 240
  • 204