我知道这被问过一百万次了,我已经尝试了我所看到的一切,但我就是无法让它发挥作用。我有数据有效负载通知。我将它们放入onMessageReceived()但是当我单击通知托盘中的通知时,它不会重定向到启动器。
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
JSONObject data = json.getJSONObject("data");
Intent resultIntent = new Intent(getApplicationContext(), LandingActivity.class);
Bundle bundle = new Bundle();
bundle.putString("data", data.toString());
resultIntent.putExtras(bundle);
// image is present, show notification with image
//showNotificationMessageWithBigImage(getApplicationContext(), title, message, "13", resultIntent, profilePicThumb);
Intent intent = new Intent(this, LandingActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, 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("FCM Message")
.setContentText("my message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
LandingActivity 是我的应用程序的启动器活动。
我的 manifest.xml
<activity
android:name=".landing.LandingActivity_"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
当给定带有可选 data_payload 的 notification_payload 时,我在 LandingActivity 中获得了数据,但是当我仅获得 data_payload 时,不会发生点击重定向
数据有效负载结构是这样的
"data" :
{
"notification_type" : "QUIZ_WINNER",
"notification_message": "You hav won the quiz ",
"notification_data" : [
"user_id" : id,
"full_name" : full_name,
"profile_pic_thumb": user_profile_picthumb,
'quiz_id' : quiz_id,
'question_id' : question_id,
'quiz_title' : quiz_title,
],
"user_id" : from_id,
'notification_to' : to_id,
'created_at' : date,
'updated_at' : date,
}
我能够收到通知,但无法打开 LandingActivity
请帮忙。提前致谢。
最终答案
最后我让它工作了..我在我的项目中使用了 android 注释。我需要打开的活动也被注释了。因此,而不是这行代码
Intent intent = new Intent(this, LandingActivity.class);
我加了这个
Intent intent = new Intent(this, LandingActivity_.class);
希望它在某个时候对某人有用。