Android - Firebase通知在点击时打开一个活动

时间:2018-02-23 17:08:25

标签: android firebase push-notification firebase-cloud-messaging

我正在尝试打开应用并在点击推送通知时显示通知数据。我到目前为止所使用的代码唯一的问题是当我有两个或更多通知时,应用程序只显示一个(收到最新通知),而不是在通知视图中显示所有单个通知。 此外,如果我收到更多通知并单击它,则应用程序将始终加载收到的第一个数据值。

我应该如何正确设置以获得所有收到的通知列表,点击其中一个会收到正确的数据?

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

我的php服务器文件: -

$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"
            ]
        ];

2 个答案:

答案 0 :(得分:2)

您需要在通知时使用不同的通知ID

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

notify (int id, Notification notification)

  

发布要在状态栏中显示的通知。如果是通知   具有相同ID的`已经由您的应用程序发布并具有   尚未取消,将被更新的信息取代。

所以

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

答案 1 :(得分:1)

由于这个原因,它只显示了一个:

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

将其更改为:

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

每个通知的通知ID都应该是唯一的。现在你不会只有一个通知,因为它们相互覆盖,这就是为什么当你点击通知时你只得到第一个值。