在前台收到的有关Firebase通知的开放活动 [英] Open activity on firebase notification received in foreground

查看:145
本文介绍了在前台收到的有关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文档:

当您的应用程序在后台运行时发出的通知.在这个在这种情况下,通知将传递到设备的系统托盘.一个用户点击通知会默认打开应用启动器.留言内容既有通知又有数据有效载荷,既有后台又有前景.在这种情况下,通知会传递到设备的系统托盘,数据有效载荷在附件中提供启动器活动的意图.

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?

扩展FirebaseMessagingService 的类 MyFirebaseMessagingService 中的方法 onMessageReceived()在前台时被正确调用,但活动没有得到开始.我也尝试过使用 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.

ForegroundActivity

ForegroundActivity

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通知的开放活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆