android– 如何在应用程序未运行时堆叠Firebase云消息传递通知?

android– 如何在应用程序未运行时堆叠Firebase云消息传递通知?,第1张

概述我正在使用FirebaseCloudMessaging将推送通知从我的服务器发送到我的Android应用程序.当应用程序运行时,通知会堆叠,因为我将它们设置为FirebaseMessagingService中的一个组.这很好.但是,当应用程序未运行时,通知不会堆叠,并且每个通知都会单独显示.这不好.即使应用程序未运行

我正在使用Firebase Cloud Messaging将推送通知从我的服务器发送到我的Android应用程序.

当应用程序运行时,通知会堆叠,因为我将它们设置为FirebaseMessagingService中的一个组.这很好.

但是,当应用程序未运行时,通知不会堆叠,并且每个通知都会单独显示.这不好.

即使应用程序未运行,如何确保通知堆叠?

这就是我的FirebaseMessagingService的样子:

public class MyFcmListenerService extends FirebaseMessagingService {    @OverrIDe    public voID onMessageReceived(RemoteMessage remoteMessage) {        RemoteMessage.Notification notification = remoteMessage.getNotification();        Uri defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)                .setSmallicon(R.drawable.notif_white)                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.notif_white))                .setContentTitle(getResources().getString(R.string.app_name))                .setContentText(notification.getbody())                .setautoCancel(true)                .setPriority(2)                .setSound(defaultSoundUri)                .setContentIntent(pendingIntent)                .setGroup("1")                .setGroupSummary(true)                .setonlyAlertOnce(true);        notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);        notificationmanager.notify(0 , notificationBuilder.build());        }    }

解决方法:

要堆叠两个或多个通知(在消息列表中指定)并使它们看起来像GMail样式通知,您可以为通知添加收件箱样式,如下所示: –

    private voID showNotification(Context mContext, String Title, List messages, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(            mContext);    NotificationCompat.InBoxStyle inBoxStyle = new NotificationCompat.InBoxStyle();    for(int i=0;i<messages.size();i++)        inBoxStyle.addline(messages.get(i));    Notification notification;    notification = mBuilder.setTicker(Title)            .setautoCancel(true)            .setContentTitle(Title)            .setContentIntent(resultPendingIntent)            .setSound(alarmSound)            .setStyle(inBoxStyle)            .setWhen(getTimeMilliSec(timeStamp))            .setSmallicon(R.drawable.notification_small_icon)            .setLargeIcon(R.drawable.notification_large_icon)            .setDeleteIntent(PendingIntent.getbroadcast(mContext,101,new Intent(mContext, NotificationdismissedReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT))            .build();    notificationmanager notificationmanager = (notificationmanager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);    notificationmanager.notify(NConfig.NOTIFICATION_ID, notification);    }

如果您注意到,我还为我的通知添加了删除意图,该意图触发NotificationdismissedReceiver(broadcastReceiver),其主要工作是清除通过滑动手势解除的通知消息,以便下次只有新通知消息堆叠在一起.

public class NotificationdismissedReceiver extends broadcastReceiver {@OverrIDepublic voID onReceive(Context context, Intent intent) {    // Todo: This method is called when the broadcastReceiver is receiving    // an Intent broadcast.    messages.clear();}}

主要逻辑是收集列表中的所有未读/未擦除通知,即消息,下面是FirebaseMessagingService的onMessageReceive(): –

    public voID onMessageReceived(RemoteMessage remoteMessage) {    Log.e(TAG, "From: " + remoteMessage.getFrom());    if (remoteMessage == null)        return;    if (remoteMessage.getData()!=null && remoteMessage.getData().size() > 0)     {        try {            JsONObject Json = new JsONObject(remoteMessage.getData().toString());            Log.e(TAG, "Notification Data: " + Json);            Title = Json.get("Title").toString();            Message = Json.get("body").toString();            messages.add(Message);        } catch (Exception e) {            Log.e(TAG, "Exception: " + e.getMessage());        }     }       showNotification(...);     }

当应用程序在前台时,FirebaseMessagingService的上述onMessageReceive()执行正常,但是当您的应用程序处于后台或被杀死时,它不会执行.
要使其执行,您必须从服务器端发送的JsON消息中省略通知部分,并且仅包括数据部分,如下所示: –

       var data = new       {           to = token,        //   notification = new        //   {        //       body = messageBody,   //Omitting notification part of data        //       Title = messageTitle,        //       icon = "myicon",        //},        data = new        {               body = messageBody,  // adding all notification information insIDe data               Title = messageTitle,               icon = "myicon",           }       };

通过这样做,您的消息现在变为仅数据消息,这意味着它将始终执行FirebaseMessagingService的onMessageReceive(),无论您的应用程序是在后台还是前台.

希望这么多解释有所帮助.

总结

以上是内存溢出为你收集整理的android – 如何在应用程序未运行时堆叠Firebase云消息传递通知?全部内容,希望文章能够帮你解决android – 如何在应用程序未运行时堆叠Firebase云消息传递通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存