4

I have a timer that creates a notification on completion. Clicking on the notification should lead to an activity called TimerActivity. I create a notification like this:

mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(completeNotificationIcon)
.setContentTitle(completeNotificationTitle)
.setContentText(completeNotificationText)
.setTicker(completeNotificationTicker)
.setAutoCancel(true);

Intent notificationIntent = new Intent(this, TimerActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

The problem is the activity doesn't open, the notification just autocancels.

On further investigation, it's only TimerActivity that won't open like this. If I change the intent to open say SttingsActivity.class, it works as it should. TimerActivity in the manifest:

    <activity   
       android:name=".TimerActivity"
       android:parentActivityName=".MainActivity"
       android:label="@string/action_timer"
       android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />           
    </activity>

But it's defined like any of these other activities that work/can be opened by clicking on a notification

TimSim
  • 3,936
  • 7
  • 46
  • 83
  • http://stackoverflow.com/questions/21804649/android-clicking-on-notification-doesnt-lead-to-activity#comment37259171_21805011 –  Oct 06 '14 at 19:48

2 Answers2

4

The solution was to add the flag FLAG_CANCEL_CURRENT to PendingIntent.

TimSim
  • 3,936
  • 7
  • 46
  • 83
0

You are trying to open an activity outside an activity you should set some flag on the intent to make it work properly. Try this code please and let me know

 mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(completeNotificationIcon)
.setContentTitle(completeNotificationTitle)
.setContentText(completeNotificationText)
.setTicker(completeNotificationTicker)
.setAutoCancel(true);

Intent notificationIntent = new Intent(this, TimerActivity.class);
       notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//single top to avoid                //creating many activity stacks queue
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
murielK
  • 1,000
  • 1
  • 10
  • 21
  • Actually, this is some nonsense bug that happens with cached intents and app reinstalls. Google's incompetence strikes again... http://code.google.com/p/android/issues/detail?id=61850 and http://code.google.com/p/android/issues/detail?id=63236 – TimSim Feb 16 '14 at 00:11
  • 4
    No, the solution was to add the flag FLAG_CANCEL_CURRENT to PendingIntent – TimSim Feb 16 '14 at 21:33
  • 1
    @TimSim can you please post that as your answer and accept it. I have been looking for almost an hour for this answer and almost missed it in your comments. – zgc7009 Jun 10 '14 at 15:51