A
A
Askar Syzdykov2015-01-08 14:09:56
Android
Askar Syzdykov, 2015-01-08 14:09:56

Why don't push notifications always open different activities?

I implement a chat in Android, when a new message arrives, I show a notification next. way:

private void showNotification(Note note) {
        Intent viewIntent = new Intent(this, ChatActivity.class);
        long roomServerId = note.getRoom().getServerId();
        viewIntent.putExtra(ChatActivity.KEY_ROOM_SERVER_ID, roomServerId);
        viewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent viewPendingIntent = PendingIntent.getActivity(this, (int) roomServerId, viewIntent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(note.getRoom().getInterlocutor().getFullName())
                        .setSound(alarmSound)
                        .setContentText(note.getText())
                        .setContentIntent(viewPendingIntent)
                        .setAutoCancel(true);
        notificationManager.notify((int) roomServerId, notificationBuilder.build());
    }

The essence of the problem: for different interlocutors I show different notifications, if you send 2 messages from different interlocutors, then 2 notifications will be displayed.
When I click on the first one, a chat with the first interlocutor opens, when I click on the second notification, a new activity is not created, but the activity with the chat of the first interlocutor remains.
But! If I open the application from the launcher, that is, MainActivity is opened, and then I repeat the steps above, then for each notification, different chat activities are opened.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Askar Syzdykov, 2015-01-08
@SyzdykovAskar

Added one more flag Intent.FLAG_ACTIVITY_CLEAR_TOP and it works:

viewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question