AggieDev
AggieDev

Reputation: 5045

Sometimes onNewIntent() isn't triggered

I have my application set up where a notification is made, and when it is clicked I want to know that in the program. Every time it is clicked the main activity opens but every now and then I run into a bug where onNewIntent(Intent intent) isn't called when the notification is clicked. Here is my onNewIntent:

@Override
    public void onNewIntent(Intent intent)  //when notification is clicked
    {
        Log.i("mydebug","Notification clicked.");

    }

and here is where the notification is created:

Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setWhen(System.currentTimeMillis())
                     .setContentTitle(schedname)
                     .setContentText("Click to deactivate this silence schedule.")
                     .setContentIntent(pendingIntent)
                     .setSmallIcon(R.drawable.ic_launcher);
                    //notification.flags |= Notification.FLAG_NO_CLEAR; //Notification cannot be clearned by user
            NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            // Uses the default lighting scheme
            builder.build().defaults |= Notification.DEFAULT_LIGHTS;
            builder.setAutoCancel(true);
            builder.setOngoing(true);
            builder.setContentIntent(pendingIntent);

            // Will show lights and make the notification disappear when the presses it
            builder.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
            mNotificationManager.notify(1, builder.build());

Upvotes: 0

Views: 569

Answers (1)

Dave
Dave

Reputation: 298

Try setting the flag to:

FLAG_ACTIVITY_CLEAR_TOP

instead of

FLAG_ACTIVITY_SINGLE_TOP

Upvotes: 1

Related Questions