22

I am using this code to show the local notification and When notification comes then on click of notification want to launch the ListActivity but on Google nexus device ListActiviy is not launches when click on notification, but on other device this code is working well.

    Intent notificationIntent = new Intent(context,
            ListActivity.class);
    notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |   Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
    PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nM = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);

    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.app_icon);
    notify.setContentTitle("Hello World");
    notify.setContentText("");
    notify.setAutoCancel(true);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notify.setSound(alarmSound);
    notify.setLights(Color.BLUE, 500, 1000);
    nM.notify(reqCode, notify.build());

Adding logcat when the activity is not launched:

03-26 14:22:35.893: W/ActivityManager(515): Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515): Unable to send startActivity intent
03-26 14:22:35.893: W/ActivityManager(515): java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1186)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:741)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:3300)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:252)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:192)
03-26 14:22:35.893: W/ActivityManager(515):     at android.content.IIntentSender$Stub.onTransact(IIntentSender.java:64)
03-26 14:22:35.893: W/ActivityManager(515):     at android.os.Binder.execTransact(Binder.java:404)
03-26 14:22:35.893: W/ActivityManager(515):     at dalvik.system.NativeStart.run(Native Method)
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
Amrit
  • 339
  • 1
  • 3
  • 10

9 Answers9

66

This is reported issue for kitkat 4.4 not opening activity when click on notification here is an issue url

http://code.google.com/p/android/issues/detail?id=63236

http://code.google.com/p/android/issues/detail?id=61850

suggested workaround is to cancel existing PendingIntent, or use PendingIntent.FLAG_CANCEL_CURRENT

OR

Try below

Adding a flag in Activity in AndroidManifiest.xml

android:exported="true"
Hardik
  • 17,179
  • 2
  • 35
  • 40
  • 7
    One of the suggestions in those links, adding *android:exported="true"* within "activity" tags for the target Activity fixed my problem on KitKat. Thank you for the links. – Kerem Apr 24 '14 at 14:57
  • 2
    PendingIntent.FLAG_CANCEL_CURRENT Fixed my problem – Qadir Hussain Jun 27 '14 at 10:03
  • 1
    This solved my issue on kitkat, however, I can only open the activity AFTER a restart of the device, has someone else encountered this? i.e before restarting the device I push on the notification and nothing happens, but after restart, if i push the button, it opens the activity – shaya ajzner Aug 22 '16 at 14:48
  • `PendingIntent.FLAG_CANCEL_CURRENT` fixed it for me on my Sony Xperia M running Android 4.3. – Sam Nov 19 '16 at 02:06
10

This issue is known for Android Kitkat 4.4 and Lollipop. Please add :

android:exported="true"

in android manifest inside activity tag which is supposed to be opened after tapping notification from status bar

satyapol
  • 983
  • 11
  • 16
3

You can also add PendingIntent.FLAG_ONE_SHOT to fix this.

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Istvan Jonyer
  • 151
  • 1
  • 9
1

Add android:exported = "true" in the manifest

<activity
            android:name=".ListActivity"
            android:label="ListActivity"
            android:exported="true">
</activity>

ListActivity is the activity which will be opened on the notification click.

manav patadia
  • 81
  • 1
  • 7
0

My above code is working well for all OS version except Kitkat 4.4 and 4.4 + But i have got solution i, put the receiver in another process and it works well for all most Android OS versions...

Like this way..

activity android:name=".NotifyReciever" android:process=":remote"

and we can learn more about processes here....

Should I use android: process =":remote" in my receiver?

Community
  • 1
  • 1
Amrit
  • 339
  • 1
  • 3
  • 10
0

The solution proposed by mass has worked for me and it's very simple, no need to change your code:

In your AndroidManifest.xml add the following attribute to your <activity> tag:

android:exported="true" 

It has worked in a Samsung Galaxy Young 2 with Android 4.4.2.

david.perez
  • 6,090
  • 4
  • 34
  • 57
0

Try to replace this

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

to

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

it will work for all version even with Kitkat also.

Note : 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.

Sonali8890
  • 2,005
  • 12
  • 16
0

If android:exported="true" doesn't work for you, just redirect the intent from your LAUNCHER activity.

In your LAUNCHER activity check for incoming notifications as follows:

 if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
        Intent redirect = new Intent(getIntent());
        redirect.setClass(this,YourNotificationActivity.class);
        startActivity(redirect);
 }

If you need to filter the intent then use:

 Intent incoming = getIntent();
 if(intent.hasExtra("type") && intent.getStringExtra("type").compareTo("notification")==0){
    Intent outgoing = new Intent(this, YourNotificationActivity.class);
    outgoing.putExtra("title", incoming.getStringExtra("title");
    outgoing.putExtra("content", incoming.getStringExtra("content");
    startActivity(outgoing);
 }
Helton Malambane
  • 1,147
  • 11
  • 12
0

I hope it will help u.

long uniqueId= System.currentTimeMillis();

    /** This is the part to let your application recognise difference notification when the user click on the notification.
     *  You could use any unique string to represent your notification. But be sure each notification have difference action name.
     **/
    taskDetailIntent.setAction("notifi" + uniqueId);


    PendingIntent contentIntent = PendingIntent.
            getActivity(this, 0, taskDetailIntent, PendingIntent.FLAG_ONE_SHOT);

    builder.setContentIntent(contentIntent);

    /** Set the unique id to let Notification Manager knows this is a another notification instead of same notification.
     *  If you use the same uniqueId for each notification, the Notification Manager will assume that is same notification
     *  and would replace the previous notification. **/
    notificationManager.notify((int) uniqueId, builder.build());