简体   繁体   English

当应用程序在后台运行时,FCM的意图不起作用(android)

[英]intent with FCM not working when app is in background(android)

I am using FCM to push notification. 我正在使用FCM推送通知。 I am passing intent to launch new activity when notification is clicked.when app is in foreground,app works fine and intent launch new activity, but when app is in background, it does not launch new activity but launch instance of default activity. 单击通知时,我正在传递启动新活动的意图。当应用程序处于前台时,应用程序可以正常运行,并且意图启动新活动,但是当应用程序处于后台时,它不会启动新活动,而是启动默认活动的实例。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional







    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SecActivity.class);
    intent.putExtra("started_from","notification");
    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("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

} }

Hope you are trying to launch the mainactivity when the message is received. 希望您在收到消息后尝试启动mainactivity。 When the app is resumed from background your current activity is getting cleared. 当应用程序从后台恢复时,您当前的活动将被清除。 From the documentation for FLAG_ACTIVITY_CLEAR_TOP: If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. 从FLAG_ACTIVITY_CLEAR_TOP的文档中:如果已设置,并且正在启动的活动已在当前任务中运行,则与其启动该活动的新实例,不关闭该活动之上的所有其他活动,并且此Intent将为作为新的Intent交付给(现在最重要的)旧活动。

Try removing this flag. 尝试删除此标志。

I too had this same problem but i managed to have it fix with this , 我也有同样的问题,但是我设法解决了这个问题,

In your default activity mentioned in the manifest do this in the onCreate 在清单中提到的默认活动中,在onCreate中执行此操作

if (bundle != null) {
    if ((String) bundle.get("tag") != null) {
        String tag = (String) bundle.get("tag");
        if (tag.equals("abc")) {
            Intent intent = new Intent(SplashActivity.this, MessageDetailsActivity.class);
            startActivity(intent);
        } else if (tag.equals("def")) {
            openSpecificActivity(tag, (String) bundle.get("id"));
        }
    } else {
        Intent i = new Intent(SplashActivity.this, HomeActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
}

用这个:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

i got a solution for that. 我有一个解决方案。 just put below code in oncreate method of launcher activity. 只需将以下代码放在启动器活动的oncreate方法中即可。

if (bundle != null) {
        String value = bundle.getString("key");
        if (value != null) {

            startActivity(new Intent(MainActivity.this, secActivity.class));
        }
}

when app is in background or killed,FCM will not call onmessagerecieved method,but it will send data to system tray to display notification.so datapayload(sent from fcm console) will not be handled by onmessagerecieved method.when user click on notification,it will launch default activity of app and datapayload will be passed by intent .so making change in oncreate method of launcher activity(as above)we can get datapayload even when app is in background or killed.(ex key is sent by fcm console).when app is in foreground datapayload and will be handled by onmessagerecieved method of fcm service. 当应用程序在后台或被杀死时,FCM不会调用onmessagerecieved方法,但会将数据发送到系统托盘以显示通知。因此datapayload(从fcm控制台发送)不会由onmessagerecieved方法处理。当用户单击通知时,它将将启动应用程序的默认活动,并通过意图传递datapayload。因此,对启动器活动的oncreate方法进行更改(如上所述),即使应用程序处于后台或终止状态,我们也可以获取datapayload(ex密钥由fcm控制台发送)。当应用程序处于前台数据有效负载时,将通过fcm服务的onmessagerecieved方法处理。

Based upon Antinio's answer 根据Antinio的回答

https://stackoverflow.com/a/37845174/4454119 https://stackoverflow.com/a/37845174/4454119

Why is this happening? 为什么会这样呢?

There are two types of messages in FCM (Firebase Cloud Messaging): FCM(Firebase云消息传递)中有两种消息类型:

display-messages: These messages trigger the onMessageReceived() callback only when your app is in foreground display-messages:仅当您的应用程序位于前台时,这些消息才会触发onMessageReceived()回调

data-messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed Firebase team have not developed a UI to send data-messages to your devices, yet. 数据消息:即使您的应用程序位于前台/后台/已被杀死的Firebase团队中,这些消息也会触发onMessageReceived()回调,但尚未开发出可将数据消息发送到您的设备的UI。

So you need to use data-messages.. 因此,您需要使用数据消息。

In FCM you have two types of messages 在FCM中,您有两种类型的消息

  • Notification Messages 通知讯息
  • Data Messages 数据信息

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. 当您希望FCM处理代表客户端应用程序显示的通知时,请使用通知消息。 Use data messages when you want to process the messages in your client app. 当您要在客户端应用程序中处理消息时,请使用数据消息。

If you need to process your message before sending it to the system tray, it's better to use Data messages, as for these types of messages, the callback first reaches the onMessageRecieved method before going to the system tray. 如果需要在将消息发送到系统托盘之前对其进行处理,最好使用数据消息,因为对于这些类型的消息,回调首先到达onMessageRecieved方法,然后再进入系统托盘。

IN YOUR SERVICE 为您服务

 "to": token, 
 "notification": {
     "title": "Title,
     "body": "Body"        
 },                    
"data" : {
     "update": "yes"
 }

IN ANDROID KOTLIN 在ANDROID KOTLIN中

val intent = Intent(this,MainActivity::class.java)
intent.putExtra("update","yes")
......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序处于后台时,FCM Intent 与数据交付在哪里? - Where is FCM Intent with data delivered, when app is in background? Android FCM:当应用程序在后台时 FCM 显示(通知)消息登录 - Android FCM: FCM display(notification) message loggin when app is in background 当应用程序处于后台状态时,FCM多个推送通知无法正常工作 - FCM Multiple push notifications not working properly when app in background state FCM for android:当应用程序处于后台时弹出系统通知 - FCM for android: popup system notification when app is in background 收到新的FCM消息(前景和背景)时,Android应用程序崩溃 - Android app crashes when receiving a new FCM message (foreground and background) Android FCM在从后台删除应用时未收到通知 - Android FCM not receiving notifications when app is removed from background Fcm:当应用程序在后台和前台时向 Android 和 IOS 发送 DataMessage - Fcm: Send DataMessage to Android and IOS when app is in background and in foreground 当app是后台FCM时,如何检索通知消息intent.getExtras() - How to retrieve notification message intent.getExtras() when app is background FCM 当应用程序处于后台-Android时,Intent会立即打开应用程序 - Intent opens the app immediately when app is in background -Android 当应用程序被杀死时,FCM 不工作 - FCM is not working when the app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM