Android - clicking on a notification does not lead to activity

I have a timer that generates a notification upon completion. Clicking on a notification should result in an activity called TimerActivity. I am creating 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 that the activity does not open, the notification just starts automatically.

With further research, it will only be TimerActivity, which will not open like that. If I change my intention to open, say, SttingsActivity.class, it works as it should. TimerActivity in 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 is defined as any of these other actions that work / can be opened by clicking on the notification

+3
source share
2 answers

The solution was to add the FLAG_CANCEL_CURRENT flag to the PendingIntent.

+4

- . , ,

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

All Articles