32

In my Service, I open up a notification on normal run, using this code:

private final static NOTIFICATION_ID = 412434;
private void startNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);
    builder.setSmallIcon(R.drawable.notification);
    builder.setContentTitle("Running");

    final Intent intent = new Intent(this, MainActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    builder.setOngoing(true);
    builder.setAutoCancel(false);

    notification = builder.build();

    startForeground(NOTIFICATION_ID, notification);
}

The PendingIntent is to open the MainActivity when the Notification is tapped. This works perfectly fine on all my test devices, using Android 2.3.3, 2.3.5 and Android 4.1.

It does not work, however on my Nexus 7 (Android 4.3), this doesn't work at all. Nothing happens when I tap on the Notification.

Did something change in the way these are put together that I missed?

Lanbo
  • 15,118
  • 16
  • 70
  • 147

2 Answers2

79

There seems to be an issue on some 4.3 devices. It can be resolved by providing a non 0 value to the requestCode parameter.

Example:

PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Markus Hi
  • 1,709
  • 18
  • 19
  • 13
    In my experience with this issue, I had to set the request code to be higher than 1000 to ensure the PendingIntent worked on the Nexus 10 running 4.3. So something like `REQUEST_CODE_BASE + myRequestCode` where `const REQUEST_CODE_BASE = 1000` seems to fix the issue on all devices I've tested. – Brett Duncavage Dec 13 '13 at 17:29
  • 5
    It was working with 0 then stopped one day switched to requestcode=1000 it works. Thx. Nexus 4, Android 4.4.2 – scottyab Apr 24 '14 at 16:01
  • 1
    +1 excellent, was having issues with this on HTC One 4.4. RQ 1 fixed for me – Dori Jul 18 '14 at 15:52
  • thank you . I have been fighting with this for the last hour or two. thanks. For what it's worth I think this is an issue for > 4.3 – prometheuspk Mar 09 '15 at 18:50
  • I put this line yesterday, and works correctly but now not work, I have the same code (works rarely why ?) –  Oct 20 '15 at 10:51
  • @delive are you sure that you don't have two notifications with the same `requestCode`? I faced it so I use a unique notification id for each notification. – Sufian Oct 21 '15 at 09:47
  • 1
    I solved my problem. with a miliseconds id, but thanks. –  Oct 21 '15 at 09:48
  • @delive I think it is the same as what I did. Just see that two subsequent notifications don't have the same `requestCode`s. – Sufian Oct 21 '15 at 09:50
2

That's worked for me:

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
contentIntent = PendingIntent.getActivity(this, randomInt, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Elnatan Derech
  • 987
  • 2
  • 12
  • 18