Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从FCM服务启动活动_Android_Firebase Cloud Messaging - Fatal编程技术网

Android 无法从FCM服务启动活动

Android 无法从FCM服务启动活动,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我正在使用fcm在我的应用程序中发送通知。我想在用户单击通知时启动MsgViewActivity。现在,当应用程序位于前台时,这项功能正常,但当应用程序未运行时,只需将我转到主活动即可。还要注意,我使用的是数据消息,因此即使在后台也会调用onMessageRecieve 这是我的FirebaseMessagingService.class代码 public class FirebaseMessagingSer extends FirebaseMessagingService { @Ov

我正在使用fcm在我的应用程序中发送通知。我想在用户单击通知时启动MsgViewActivity。现在,当应用程序位于前台时,这项功能正常,但当应用程序未运行时,只需将我转到主活动即可。还要注意,我使用的是数据消息,因此即使在后台也会调用onMessageRecieve

这是我的FirebaseMessagingService.class代码

public class FirebaseMessagingSer extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);

        Intent intent = new Intent(this, MsgViewActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        if(remoteMessage.getData().size()>0) {
            String id = remoteMessage.getData().get("id");
            Bundle bundle = new Bundle();
            bundle.putString("id",id);
            intent.putExtras(bundle);
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("FCM NOTIFICATION");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setLargeIcon(bmp);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());


    }
}

在您的示例中,您使用的消息同时具有
通知:{..}
数据:{..}
有效负载

这意味着您的消息被视为一条
通知消息
,因此只有当应用程序位于前台时,才会执行
onMessageReceived()
方法。(这就解释了你的问题)

您有两种解决方案:

  • 仅使用数据消息。不要在通知中发送任何内容:{..}消息的一部分。您可以将body和title设置为数据有效负载中的附加键值

  • 您可以将通知消息与参数click\u action=“intent\u to\u activity2”一起使用。
    您还需要将意图过滤器添加到活动清单中