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

  1. <legend id='Fy7na'><style id='Fy7na'><dir id='Fy7na'><q id='Fy7na'></q></dir></style></legend>
    • <bdo id='Fy7na'></bdo><ul id='Fy7na'></ul>

  2. <tfoot id='Fy7na'></tfoot>
    1. <i id='Fy7na'><tr id='Fy7na'><dt id='Fy7na'><q id='Fy7na'><span id='Fy7na'><b id='Fy7na'><form id='Fy7na'><ins id='Fy7na'></ins><ul id='Fy7na'></ul><sub id='Fy7na'></sub></form><legend id='Fy7na'></legend><bdo id='Fy7na'><pre id='Fy7na'><center id='Fy7na'></center></pre></bdo></b><th id='Fy7na'></th></span></q></dt></tr></i><div id='Fy7na'><tfoot id='Fy7na'></tfoot><dl id='Fy7na'><fieldset id='Fy7na'></fieldset></dl></div>
    2. 在前台收到的关于 Firebase 通知的开放活动

      时间:2023-07-29
          <tfoot id='pDRVN'></tfoot>

          • <small id='pDRVN'></small><noframes id='pDRVN'>

              <tbody id='pDRVN'></tbody>

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

                <bdo id='pDRVN'></bdo><ul id='pDRVN'></ul>

                本文介绍了在前台收到的关于 Firebase 通知的开放活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我的应用程序打开并收到通知时,我希望能够立即打开关联的活动,而无需用户点击通知.

                When my application is open and I receive a notification I want to be able to open the activity associated immediately without the need of the user to tap on the notification.

                这个问题非常相似:收到 Firebase 通知时打开应用程序 (FCM)

                但它在后台打开应用程序,我需要在我的应用程序在前台时这样做.

                But it opens the app when it is in background, I need to do it when my app is in foreground.

                来自 firebase 文档:

                当您的应用在后台时发送的通知.在这个在这种情况下,通知会发送到设备的系统托盘.一个默认情况下,用户点击通知会打开应用启动器.留言具有通知和数据有效负载,背景和前景.在这种情况下,通知将发送到设备的系统托盘,数据负载在附加组件中交付启动器 Activity 的意图.

                Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

                这是我对 onMessageReceived

                @Override
                    public void onMessageReceived(RemoteMessage remoteMessage) {
                
                       // Check if message contains a data payload.
                        if (remoteMessage.getData().size() > 0) {
                            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                        }
                
                        // Check if message contains a notification payload.
                        if (remoteMessage.getNotification() != null) {
                            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
                            sendNotification( remoteMessage);              
                        }     
                    }
                
                    /**
                     * Create and show a simple notification containing the received FCM message.
                     *
                     * @param remoteMessage FCM message message received.
                     */
                    private void sendNotification(RemoteMessage remoteMessage) {
                        Intent intent = new Intent(this, MyActivity.class);
                
                        Map<String, String> hmap ;
                        hmap = remoteMessage.getData();
                        hmap.get("data_info");
                        intent.putExtra("data_info", hmap.get("data_info"));
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                
                
                        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                                PendingIntent.FLAG_ONE_SHOT);
                
                    }
                

                我能够正确收到通知,但只有在我点击系统托盘上的通知后才会开始活动.

                I am able to get the notification correctly but the activity only starts once I tap the notification on the system tray.

                有没有办法在前台不点击通知来启动活动?

                Is there a way to start the activity without tapping the notification while in foreground?

                MyFirebaseMessagingService 类中的 onMessageReceived() 方法 extends FirebaseMessagingService 在前台被正确调用,但活动没有得到开始了.我也尝试过使用标志 FLAG_ACTIVITY_NEW_TASK 也没有运气.提前致谢.

                The method onMessageReceived() from the class MyFirebaseMessagingService that extends FirebaseMessagingService is getting called correctly while in foreground, but the activity is not getting started. I have also tried with the flag FLAG_ACTIVITY_NEW_TASK also with no luck. Thanks in advance.

                推荐答案

                您可以在前台活动中注册广播接收器并从您的 onReceiveMessage() 方法发送广播.

                You can achieve that registering a broadcast receiver in you foreground activity and sending a broadcast from your onReceiveMessage() method.

                前台活动

                mReceiver = new BroadcastReceiver() {
                 @Override
                 public void onReceive(Context context, Intent intent) {
                     Intent myNewActivity = new Intent(this, MyActivity.class);
                     startActivity(myNewActivity);
                   }
                 };
                
                mIntentFilter=new IntentFilter("OPEN_NEW_ACTIVITY");
                
                @Override
                protected void onResume() {
                     super.onResume();
                     registerReceiver(mReceiver, mIntentFilter);
                }
                
                
                
                @Override
                protected void onPause() {
                     if(mReceiver != null) 
                            unregisterReceiver(mReceiver);
                            mReceiver = null;
                     }
                     super.onPause();
                   }
                

                FirebaseNotificationReceiver

                FirebaseNotificationReceiver

                @Override
                    public void onMessageReceived(RemoteMessage remoteMessage) {
                
                   // Check if message contains a data payload.
                    if (remoteMessage.getData().size() > 0) {
                        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                    }
                
                    // Check if message contains a notification payload.
                    if (remoteMessage.getNotification() != null) {
                        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
                        sendNotification( remoteMessage);  
                
                        Intent broadcast = new Intent();
                        broadcast.setAction("OPEN_NEW_ACTIVITY);
                        sendBroadcast(broadcast);
                    }     
                }
                

                您可以添加检查以了解 应用是否在前台 或不选择发送通知或发送广播.

                You can add a check to know if the app is in foreground or not to choose between send a notification or send a broadcast.

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

                上一篇:Firebase 通知是灰色的 下一篇:带有 FCM 令牌的设备的 Firebase 通知说已发送但未收到

                相关文章

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

              • <small id='2lJmD'></small><noframes id='2lJmD'>

                  <bdo id='2lJmD'></bdo><ul id='2lJmD'></ul>
                  1. <legend id='2lJmD'><style id='2lJmD'><dir id='2lJmD'><q id='2lJmD'></q></dir></style></legend>
                  2. <tfoot id='2lJmD'></tfoot>