简体   繁体   English

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

[英]Android - Firebase Notification open an Activity when clicked

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:-我的 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"
            ]
        ];

You need to use different notification id while notifying您需要在通知时使用不同的通知 ID

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

notify (int id, Notification notification) 通知(int id,通知通知)

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.如果您的应用程序已经发布了具有相同 id 的通知并且尚未取消,它将被更新的信息替换。

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

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.每个通知的通知 ID 应该是唯一的。 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.现在您不会只有一个通知,因为它们会相互覆盖,这就是为什么当您单击通知时,您只会获得第一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击带有Firebase的通知时打开通知活动 - Open Notification activity when clicked on Notification with firebase 在Android中单击Firebase通知时将数据发送到活动 - Send data to activity when Firebase notification is clicked in Android 单击通知后,打开一个活动 - When a notification's clicked, open an activity 在Firebase RecyclerView Cardview中单击项目时如何打开详细信息活动 - How to open a details activity when an item is clicked in Firebase RecyclerView Cardview 在Android 4.0版上单击通知时,永远不会启动Activity - Activity is never launched when the notification is clicked on android version 4.0 单击Firebase Notification时,打开MainActivity以外的其他活动 - Open a different activity other than MainActivity when clicking Firebase Notification Android通知未开启活动? - Android notification dont open activity? 如何在片段中打开活动页面,当按下 android 中的通知时 - How to open activity page in fragment, when pressed a notification in android 当我点击通知推送时,特定活动无法打开(需要真正的帮助) - When i push notification clicked, specific activity doesn't open (need really help) Firebase 如何在 Android 中打开其他活动时删除 ValueEventListeners - Firebase how to ValueEventListeners remove when other activity open in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM