使用Android Firebase堆叠推送通知 [英] Stack push notifications with Android Firebase

查看:225
本文介绍了使用Android Firebase堆叠推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了Android应用程序,该应用程序使用Firebase接收推送通知.我的代码基于Firebase/Google官方文档( https://firebase.google. com/docs/cloud-messaging/android/client https://firebase.google.com/docs/cloud-messaging/android/receive ),并没有什么特别之处,它具有扩展FirebaseMessagingService的服务和扩展FirebaseInstanceIdService的服务.一切正常.

I've developed and Android app that receives Push Notifications using Firebase. My code is based on Firebase/Google official docs (https://firebase.google.com/docs/cloud-messaging/android/client and https://firebase.google.com/docs/cloud-messaging/android/receive) and has nothing special, it has the service that extends FirebaseMessagingService and the service that extends FirebaseInstanceIdService. Everything is working ok.

但是此应用程序收到的通知比预期的要多,我的客户希望该应用程序堆叠收到的通知. 我看过有关解决方案的教程,该解决方案适用于旧的GCM机制,但FCM却一无所获.所以我的怀疑是: 是否可以与FCM堆叠收到的推送通知?还是以某种方式在后端进行编码?

But this app is receiving more notifications than expected and my client wants the app to stack the notifications it receives. I've seen tutorials with the solution working over the old GCM mechanism but nothing with FCM. So my doubts are: is possible to stack received push notifications with FCM? or has this to be somehow coded in the backend?

推荐答案

您只需要将.setGroup(GROUP_KEY_XPTO)添加到通知生成器中.具有相同组ID的所有通知将被堆叠.

You just need to add .setGroup(GROUP_KEY_XPTO) to your notification builder. All the notifications with the same group id will be stacked.

final static String GROUP_KEY_EMAILS = "group_key_emails";

// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender1)
         .setContentText(subject1)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

// Issue the notification
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);

关于下一个通知:

Notification notif2 = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender2)
         .setContentText(subject2)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

notificationManager.notify(notificationId2, notif2);

在此处查看有关此内容的更多信息: https://developer.android.com/training/wearables/notifications/stacks.html

See more about it here: https://developer.android.com/training/wearables/notifications/stacks.html

这篇关于使用Android Firebase堆叠推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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