Android - Firebase Notification open an Activity w

2019-07-28 23:23发布

问题:

I am trying to open the app and show the notification data when a push notification is clicked. The code I have so far is working the only problem is that when I have two or more notifications the app shows only one ( the latest notification received) instead of showing all the single notifications in the notification view. Also if I have more notifications received and click on it, the app loads always the first data value received.

How should I setup this properly in order to have a list of all the notifications received and when click on one of the will load the correct data?

private void sendNotification(String image, String title, String subtitle, String content, String url, String time, String category) {
        //RemoteMessage.Notification notification = remoteMessage.getNotification();
        Intent intent = null;
        PendingIntent pendingIntent;
        if (url.equals("")) { // Simply run your activity
            intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else { // open a page
            if (!url.equals("")) {
                intent = new Intent(this, NewsNotificationActivity.class);
                intent.putExtra("image", image);
                intent.putExtra("title", title);
                intent.putExtra("subtitle", subtitle);
                intent.putExtra("content", content);
                intent.putExtra("url", url);
                intent.putExtra("time", time);
                intent.putExtra("category", category);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            }
        }
        pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.loading_icon)
                //.setContentTitle(notification.getTitle())
                .setContentTitle(title)
                .setContentText(subtitle)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }
}

my php server file:-

$fields = [
            'to'           => '/topics/test2',
            'data' => [
                "image" => "https://example.com/image.png",
                "title" => "aaaaaaa",
                "subtitle" => "bbbbbbb",
                "content" => "ccccc",
                "url" => "https://example.com/news90789/",
                "time" => "dddd",
                "category" => "rrrrrr"
            ]
        ];

回答1:

It shows only one because of this:

notificationManager.notify(0, notificationBuilder.build());

Change it to this:

int num = (int) System.currentTimeMillis();
notificationManager.notify(num, notificationBuilder.build());

The notification id should be unique for each notification. Now you won't have only one notification as they were overriding each other and that is why when you clicked the notification you only got the first value.



回答2:

You need to use different notification id while notifying

notificationManager.notify(0, notificationBuilder.build());
// should be different.   ^^

notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification `with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

so

int id = 0; 
notificationManager.notify(id++, notificationBuilder.build());
// should be different.   ^^
// you can use shared preference to keep track of id 
// so that in future you can cancel it via java code