在前台收到的对firebase通知的打开活动

ban*_*ode 9 android firebase firebase-cloud-messaging firebase-notifications

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

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

但它在应用程序处于后台时打开应用程序,我需要在应用程序处于前台时执行此操作.

firebase文档:

当您的应用在后台时发送通知.在这种情况下,通知将传递到设备的系统托盘.用户点按通知会默认打开应用启动器.具有通知和数据有效负载的消息,包括后台和前台.在这种情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递.

这是我的命令 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);

    }
Run Code Online (Sandbox Code Playgroud)

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

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

该方法onMessageReceived()从类MyFirebaseMessagingServiceextends FirebaseMessagingService获取调用,而在前台正常,但活动中,无法启动.我也试过国旗FLAG_ACTIVITY_NEW_TASK也没有运气.提前致谢.

小智 5

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

前台活动

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();
   }
Run Code Online (Sandbox Code Playgroud)

Firebase 通知接收器

@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);
    }     
}
Run Code Online (Sandbox Code Playgroud)

您可以添加检查以了解应用程序是否处于前台或不选择发送通知或发送广播。


ban*_*ode 1

我可以通过在pendingIntent上调用send()来实现这一点:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
try {
       pendingIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)