• <legend id='Mj3QV'><style id='Mj3QV'><dir id='Mj3QV'><q id='Mj3QV'></q></dir></style></legend>

    <tfoot id='Mj3QV'></tfoot>
      <bdo id='Mj3QV'></bdo><ul id='Mj3QV'></ul>

      <i id='Mj3QV'><tr id='Mj3QV'><dt id='Mj3QV'><q id='Mj3QV'><span id='Mj3QV'><b id='Mj3QV'><form id='Mj3QV'><ins id='Mj3QV'></ins><ul id='Mj3QV'></ul><sub id='Mj3QV'></sub></form><legend id='Mj3QV'></legend><bdo id='Mj3QV'><pre id='Mj3QV'><center id='Mj3QV'></center></pre></bdo></b><th id='Mj3QV'></th></span></q></dt></tr></i><div id='Mj3QV'><tfoot id='Mj3QV'></tfoot><dl id='Mj3QV'><fieldset id='Mj3QV'></fieldset></dl></div>

      <small id='Mj3QV'></small><noframes id='Mj3QV'>

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

        时间:2023-07-30

          • <legend id='wrhSk'><style id='wrhSk'><dir id='wrhSk'><q id='wrhSk'></q></dir></style></legend>
          • <tfoot id='wrhSk'></tfoot>

              <small id='wrhSk'></small><noframes id='wrhSk'>

                <bdo id='wrhSk'></bdo><ul id='wrhSk'></ul>
              • <i id='wrhSk'><tr id='wrhSk'><dt id='wrhSk'><q id='wrhSk'><span id='wrhSk'><b id='wrhSk'><form id='wrhSk'><ins id='wrhSk'></ins><ul id='wrhSk'></ul><sub id='wrhSk'></sub></form><legend id='wrhSk'></legend><bdo id='wrhSk'><pre id='wrhSk'><center id='wrhSk'></center></pre></bdo></b><th id='wrhSk'></th></span></q></dt></tr></i><div id='wrhSk'><tfoot id='wrhSk'></tfoot><dl id='wrhSk'><fieldset id='wrhSk'></fieldset></dl></div>
                  <tbody id='wrhSk'></tbody>

                  本文介绍了如何在应用程序未运行时堆叠 Firebase 云消息传递通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I am using Firebase Cloud Messaging to send push notifications from my server to my android application.

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

                  When the application is running, the notifications are stacked because I set them to a group in my FirebaseMessagingService. Which is nice.

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

                  However, when the application is not running, the notifications are not stacked and each one appears individually. Which is not nice.

                  即使应用程序没有运行,如何确保通知是堆叠的?

                  How to make sure notifications are stacked even when the application is not running?

                  这就是我的 FirebaseMessagingService 的样子:

                  This is how my FirebaseMessagingService looks like:

                  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());
                  
                          }
                      }
                  

                  推荐答案

                  当您的应用在后台或被杀死时,Firebase 不会调用您的 onMessageReceived,并且您无法自定义通知.将显示系统生成的通知.

                  Firebase will not call your onMessageReceived when your app is in the background or killed, and you can't customise your notification. System generated notification will show.

                  让 Firebase 库在每种情况下都调用您的 onMessageReceived

                  to make Firebase library to call your onMessageReceived in every case

                  a) 前景

                  b) 背景

                  c) 被杀死

                  您不得将 JSON 密钥通知";在您对 firebase API 的请求中,而是使用数据",见下文.

                  you must not put JSON key "notification" in your request to firebase API but instead use "data", see below.

                  例如,下面的消息不会调用onMessageReceived()

                  For example, following message will not call onMessageReceived()

                  {
                    "to": "/topics/test",
                    "notification": {
                      "title" : "title",
                      "message": "data!"
                     }
                  }
                  

                  但这会起作用

                  {
                    "to": "/topics/test",
                     "data": {
                         "title":"title",
                         "message":"data!"
                     }
                  } 
                  

                  参见 this 它有对 firebase 消息类型的详细描述例如:

                  see this it has a detailed description of firebase message type For example:

                  @Override
                  public void onMessageReceived(RemoteMessage remoteMessage) {
                  
                      Log.d(TAG, "From: " + remoteMessage.getFrom());
                  
                      // Check if message contains a data payload.
                      if (remoteMessage.getData().size() > 0) {
                          Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                          sendNotification(remoteMessage.getData().get("message").toString(), remoteMessage.getData().get("title").toString());
                      }
                  
                  }
                  
                  private void sendNotification(String message, String title) {
                          int requestID = (int) System.currentTimeMillis();
                          Intent intent = new Intent(this, activityCompat);
                          
                          PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                  
                          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                  .setSmallIcon(R.drawable.small_logo)
                                  .setContentTitle(title)
                                  .setContentText(message).setContentIntent(pendingIntent)
                                  .setAutoCancel(true)
                                  .setStyle(new NotificationCompat.BigTextStyle()
                                          .bigText(messageBody))
                                  .setTicker(messageBody);
                  
                          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                          notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
                          Notification notification = notificationBuilder.build();
                  
                          notificationManager.notify(0, notification);
                  
                  }
                  

                  这篇关于如何在应用程序未运行时堆叠 Firebase 云消息传递通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android:订阅 Firebase Cloud Messaging(FCM) 主题 下一篇:FCM - 以编程方式向用户细分发送推送通知

                  相关文章

                    <bdo id='1CJmA'></bdo><ul id='1CJmA'></ul>
                      <legend id='1CJmA'><style id='1CJmA'><dir id='1CJmA'><q id='1CJmA'></q></dir></style></legend>

                      <i id='1CJmA'><tr id='1CJmA'><dt id='1CJmA'><q id='1CJmA'><span id='1CJmA'><b id='1CJmA'><form id='1CJmA'><ins id='1CJmA'></ins><ul id='1CJmA'></ul><sub id='1CJmA'></sub></form><legend id='1CJmA'></legend><bdo id='1CJmA'><pre id='1CJmA'><center id='1CJmA'></center></pre></bdo></b><th id='1CJmA'></th></span></q></dt></tr></i><div id='1CJmA'><tfoot id='1CJmA'></tfoot><dl id='1CJmA'><fieldset id='1CJmA'></fieldset></dl></div>
                    1. <tfoot id='1CJmA'></tfoot>

                      <small id='1CJmA'></small><noframes id='1CJmA'>