prashantwosti
prashantwosti

Reputation: 1010

Pending intent in notification not working

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());

}

Upvotes: 51

Views: 63216

Answers (6)

Rick Royd Aban
Rick Royd Aban

Reputation: 906

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.

Upvotes: 3

live-love
live-love

Reputation: 52534

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.

Upvotes: 2

Zeero0
Zeero0

Reputation: 2790

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...
}

Upvotes: 2

droidx
droidx

Reputation: 2172

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());

}

Upvotes: 89

Ravi Gadipudi
Ravi Gadipudi

Reputation: 1455

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);

Upvotes: 7

pratsJ
pratsJ

Reputation: 3443

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.

Upvotes: 13

Related Questions