FCM通知点击无法打开预期的活动

3
我尝试了这里发布的几乎所有解决方案和每个标志组合,但都没有起作用。
以下是我遇到问题的用例。
1)当我在应用程序中使用FCM通知时,打开我想要的活动。数据传递到主活动中的onNewIntent。当应用程序处于前台时运行良好。
2)当处于后台模式(按下主页按钮)时,单击通知后,它会启动我的应用程序的新实例,即使我已在清单文件中将android:launchMode =“singleTop”指定为我的主要活动,但我不知道这里正在发生什么。我想要在此处打开的期望活动是RateEventActivity。
这里发生的奇怪的事情是,即使应用程序在后台模式下,通知活动和个人资料活动也可以正常工作,但是RateEventActivity作为我上面描述的所有问题产生了问题。大部分应用程序代码由其他人编写, 我正在处理RateEevent模块,因此我不知道我错过了什么?
在RateEventActivity场景中,当我从后台模式中单击通知时,意图会丢失(在创建时未传递给mainActivty)。
我还尝试了将唯一ID传递给待定意图,但没有成功。我投入了大量时间和精力来解决这个问题,但每次都失败了。
以下是我的FCM代码。
  private void createNotification(RemoteMessage remoteMessage) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.v("Notification Title", remoteMessage.getNotification().getTitle());

    Intent intent = new Intent(this, MainActivity.class);
    //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    if (remoteMessage.getData().containsKey("notificationUser")) {
        intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
    } else if (remoteMessage.getData().containsKey("notificationId")) {
        intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
    } else if (remoteMessage.getData().containsKey("event")) {
        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {


            Log.v("Notification Event", remoteMessage.getData().get("event"));
            intent.putExtra("eventId", remoteMessage.getData().get("event"));
        }


    }


    //PendingIntent.FLAG_UPDATE_CURRENT
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

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

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setLargeIcon(largeIcon)
                    .setContentTitle(notification.getTitle())
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(notification.getBody()))
                    .setContentText(notification.getBody())
                    .setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

主Activity代码onCreate方法

   if (getIntent().hasExtra("notificationId")) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
        startActivity(intent);
    } else if (getIntent().hasExtra("user")) {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.putExtra("userId", getIntent().getStringExtra("user"));
        startActivity(intent);
    } else if (getIntent().hasExtra("eventId")) {
        Intent intent = new Intent(this, RateEventActivity.class);
        intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
        startActivity(intent);
    }

我的清单文件

 <activity
        android:name=".activities.MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/HomeTheme" />

<activity
        android:name=".activities.ProfileActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Profile"
        android:screenOrientation="portrait" />

 <activity
        android:name=".activities.NotificationActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Notifications"
        android:screenOrientation="portrait" />

<activity
        android:name=".activities.RateEventActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Rate Users"
        android:screenOrientation="portrait"
       />

你有获取到这个日志吗?Log.v("Notification Event", remoteMessage.getData().get("event")); - sohan shetty
是的,我正在从推送通知中获取数据。 - user2991828
2个回答

5
如果您想在应用程序处于后台时打开应用并执行特定操作,请在通知负载中设置click_action并将其映射到要启动的Activity中的intent过滤器。例如,将click_action设置为OPEN_ACTIVITY_1以触发以下意图过滤器:
如react-native-fcm文档中建议的那样,请请求后端以此形式发送json数据。
 {

    "to":"some_device_token",

    "content_available": true, 
    "notification": {
    "title": "hello",
    "body": "yo",
    "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
    },

"data": {
"extra":"juice"
}
}

在您的清单文件中,为您的活动添加以下意图过滤器

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

当您点击通知时,它将打开应用并直接进入您在click_action中定义的活动,例如"OPEN_ACTIVITY_1"。在该活动中,您可以通过以下方式获取数据:
Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");

查看以下链接以获取更多帮助:

Firebase FCM通知click_action负载

当应用程序在后台运行时,Firebase onMessageReceived未被调用

Firebase控制台:如何为通知指定click_action


+1 是关于使用动作和类别设置意图过滤器以使 click_action 生效的解释。这对我帮助很大。 - Felix Htoo

0

我很久以前就遇到了这个问题。让我检查一下我的项目代码。

啊,它在这里。

尝试设置标志。

Intent intent = new Intent(this,    MainActivity.class));       
// set intent so it does not start a new activity
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);

并将您的PendingIntent更改如下

PendingIntent contentIntent = PendingIntent.getActivity(this, ,
                intent, 0);

同时,移除

android:launchMode="singleTop"

从你的MainActivity文件中的Manifest文件

尝试这个,让我们看看它是否有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接