【问题标题】:Android - Firebase Notification open an Activity when clickedAndroid - 点击时 Firebase 通知会打开一个 Activity
【发布时间】:2018-02-23 17:08:25
【问题描述】:

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

我应该如何正确设置才能获得收到的所有通知的列表,并且在单击其中一个时会加载正确的数据?

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

【问题讨论】:

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


    【解决方案1】:

    通知时需要使用不同的通知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
    

    【讨论】:

    • 谢谢我现在有更多通知。但是,我点击时的数据总是显示第一个收到的
    • 你能检查这些变量的值String image, String title, String subtitle,确保它们不同,使用日志来检查值
    • 您的意思是与两个通知中收到的数据不同?
    • 是的,确保每个通知的数据都不同
    【解决方案2】:

    因为这个,它只显示一个:

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

    改成这样:

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

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

    【讨论】:

    • 谢谢,这行得通,我不知道如何显示不同的数据,因为它仍然显示收到的第一个通知的数据
    • @SNos 你收到intent.putExtra("image", image); intent.putExtra("title", title);.. 活动中的那些数据了吗?它不应该显示第一个。你是点击通知然后应用打开,然后你点击另一个通知?
    • 是的,我在名为 NewsNotification 的活动中收到了intent.putExtra("image", image); intent.putExtra("title", title);..。我收到通知,点击它打开应用程序并显示第一个。现在,如果我单击第二个,则不会发生任何事情。或者,如果应用程序已关闭并且我单击收到的第二个通知,则会显示第一个通知的值
    • 在您的 php 服务器中,您发送的是相同的数据吗?您是否总是在标题"aaaaaaa" 中发送此内容?
    • @SNos 服务器中的数据需要是用户输入的变量。如果是这样的"aaaaaaa", 或者即使您将其更改为bbbbb,您也将始终收到
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多