gpt4 book ai didi

android - 无法从 FCM 服务启动 Activity

转载 作者:行者123 更新时间:2023-11-30 00:59:21 30 4
gpt4 key购买 nike

我正在使用 fcm 在我的应用程序中发送通知。我想在用户单击通知时启动 MsgViewActivity。现在,当应用程序处于前台时,这工作得很好,但当应用程序未运行时,它只是将我带到主要 Activity 。另请注意,我正在使用数据消息,因此即使在后台也会调用 onMessageRecived。

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


}
}

最佳答案

在您的示例中,您使用的消息同时包含 notification: {..}data: {..} 有效负载。

这意味着您的消息被视为 notification-message,因此方法 onMessageReceived() 仅在应用程序处于前台时执行。 (这解释了你的问题)

您有两个解决方案:

  1. 仅使用数据消息。不要在通知中发送任何内容:消息的 {..} 部分。您可以将正文和标题设置为数据负载中的附加键值。

  2. 您可以使用带参数 click_action="intent_to_activity2"的通知消息。
    您还需要将 Intent 过滤器添加到您的 Activity list 中。

关于android - 无法从 FCM 服务启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669095/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com