<legend id='eOTbt'><style id='eOTbt'><dir id='eOTbt'><q id='eOTbt'></q></dir></style></legend>
  • <small id='eOTbt'></small><noframes id='eOTbt'>

  • <tfoot id='eOTbt'></tfoot>

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

        • <bdo id='eOTbt'></bdo><ul id='eOTbt'></ul>

        Firebase 推送通知

        时间:2023-07-29

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

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

                2. 本文介绍了Firebase 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 Android 应用中为推送通知实现了 Firebase.我实现了两项服务来注册令牌并在检测到通知时创建通知.当我的应用程序启动时它正在工作,但是当我的应用程序关闭时它不起作用.

                  I implemented Firebase for push notification in my Android app. I implemented two services for register the token and for create the notification when it is detected. When my app is launch it's working but when my app is closed it doesn't working.

                  public class FirebaseinstanceIdService extends FirebaseInstanceIdService {
                  
                      @Override
                      public void onTokenRefresh() {
                          String refreshedToken = FirebaseInstanceId.getInstance().getToken();
                          Log.e("Firebase", refreshedToken);
                          MainActivity.setFirebaseToken(refreshedToken);
                      }
                  
                  }
                  
                  public class MyFirebaseMessageService extends FirebaseMessagingService {
                  
                      private static final String TAG = "MyFirebaseMsgService";
                  
                      @Override
                      public void onMessageReceived(RemoteMessage remoteMessage) {
                          //Displaying data in log
                          //It is optional
                          Log.e(TAG, "From: " + remoteMessage.getFrom());
                          Log.e(TAG, "Notification Message Body: " + remoteMessage.getData().get("title"));
                  
                          //Calling method to generate notification
                          sendNotification(remoteMessage.getData());
                      }
                  
                      //This method is only generating push notification
                      //It is same as we did in earlier posts
                      private void sendNotification(Map<String, String> notification) {
                          Intent intent = new Intent(this, MainActivity.class);
                          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                              PendingIntent.FLAG_ONE_SHOT);
                  
                          Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                              .setSmallIcon(R.mipmap.ic_launcher)
                              .setContentTitle(notification.get("title"))
                              .setContentText(notification.get("body"))
                              .setAutoCancel(true)
                              .setSound(defaultSoundUri)
                              .setContentIntent(pendingIntent);
                  
                           NotificationManager notificationManager =
                              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                  
                          notificationManager.notify(0, notificationBuilder.build());
                      }
                  }
                  

                  还有我的清单:

                      <service
                          android:name=".FirebaseinstanceIdService">
                          <intent-filter>
                              <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                          </intent-filter>
                      </service>
                      <service
                          android:name=".MyFirebaseMessageService">
                          <intent-filter>
                              <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                          </intent-filter>
                      </service>
                  

                  还有我的要求:

                  {
                      "to": "ex1CtVz5bbE:APA91bHiM_BCun62A9iCobF1yV26Dwn-hYCDhkYPoWEG5ZdqH0P28UsE5q1v7QibwAX7AJ290-9en9L6f548_2b7WEbJ8RPEWlIotLczjjCP7xEOWqeJk6Iz44vilWYvdu4chPwfsvXD",
                      "data": {
                          "title": "Notification",
                          "body": "Message",
                      }
                  }
                  

                  我已经在 StackOverflow 中找到了一些解决方案,但在我的情况下它不起作用.我有一个 API Rest 调用 API Request Post:https://fcm.googleapis.com/fcm/send

                  I already found a few solutions in StackOverflow but it doesn't work in my case. I have an API Rest which call the API Request Post : https://fcm.googleapis.com/fcm/send

                  所以,我的问题是:Firebase 是否在应用关闭时处理推送通知以及如何处理?

                  So, there is my question : Does Firebase handle push notification when the app is closed and how to ?

                  感谢您的回答

                  推荐答案

                  FCM 确实支持应用关闭时的通知.您的代码似乎没问题,所以我想电池节能器(或节电器)可以杀死您的通知.我在华硕 Zenfone 上遇到过这样的问题,在使用华为和小米的情况下也有报告.只需禁用它们或将您的应用程序添加到例外列表中(如果有的话).在最近的 Android 版本中还有一个新的省电模式,请尝试禁用它.

                  FCM does support notifications when app is closed. Your code seems to be OK, so I suppose battery economizers (or power savers) can kill your notifications. I had such problems on my Asus Zenfone, also they were reported in cases of using Huawei and Xiaomi. Just disable them or add your app in exception list, if there is one. Also there is a new power-saving mode in recent releases of Android, try to disable it too.

                  这篇关于Firebase 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Firebase FCM 推送,出现错误缺少注册 |安卓 下一篇:如何使用 Firebase 17.0.1 执行 INSTANCE_ID_EVENT 操作?

                  相关文章

                  • <bdo id='WiaBu'></bdo><ul id='WiaBu'></ul>

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

                    <tfoot id='WiaBu'></tfoot>

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