使用Android Firebase堆叠推送通知

时间:2016-10-20 15:40:52

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

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

但是这个应用程序收到的通知超出预期,我的客户希望应用程序堆叠收到的通知。 我已经看到教程解决方案在旧的GCM机制上工作但FCM没有。所以我的怀疑是: 是否可以使用FCM堆叠收到的推送通知?或者以后以某种方式在后端编码?

3 个答案:

答案 0 :(得分:0)

堆叠通知可以使用FCM完成,它也需要在服务器端进行一些更改。所有这些都在这里详细解释: -

https://stackoverflow.com/a/43913156/6157185

答案 1 :(得分:0)

 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
    Notification mNotification = builder
            .setLargeIcon(image)/*Notification icon image*/
            .setContentText(messageBody)
            .setSmallIcon(R.drawable.dhlone)
            .setContentTitle("DHL")
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image).bigLargeIcon(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
           .setBadgeIconType(R.drawable.dhlone)
            .setAutoCancel(true)

//使用设置setAutoCancel(true)。会工作

答案 2 :(得分:-3)

您只需将.setGroup(GROUP_KEY_XPTO)添加到通知构建器即可。所有具有相同组ID的通知都将被堆叠。

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