Android-Firebase Notification单击时打开一个活动 [英] Android - Firebase Notification open an Activity when clicked

查看:245
本文介绍了Android-Firebase Notification单击时打开一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的php服务器文件:-

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

推荐答案

由于以下原因,它仅显示了一个:

It shows only one because of this:

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

将其更改为此:

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

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

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.

这篇关于Android-Firebase Notification单击时打开一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆