【发布时间】:2023-04-08 09:03:02
【问题描述】:
我在通知和待处理的 Intent 方面遇到了很大的麻烦。我正在尝试使用发送消息的适当 user_details 打开聊天活动。这就是为什么在 Firebase 函数上我传递了 from_user_id ,它是发送消息的人。我在 FCM 中获得了正确的日志,但是当我收到聊天通知并打开它时,它会打开没有任何用户名和消息的活动。它使用默认值打开一个新的活动实例。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.chitchat_icon)
.setContentTitle(notification_title)
.setAutoCancel(true)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
mBuilder.setChannelId(CHANNEL_ID);
}
mNotificationManager.notify(mNotificationId,mBuilder.build());
}
消息负载:
const payload = {
notification: {
title: userName,
body: message,
icon: "default",
click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};
我的清单如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chitchat">
...
<application
android:name=".ChitChat"
android:allowBackup="true"
android:icon="@drawable/chitchat_icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".GroupChatActivity"></activity>
<activity android:name=".CallActivity" />
<activity
android:name=".ChatActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="com.example.chitchat_TARGET_MESSAGE_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ProfileActivity">
<intent-filter>
<action android:name="com.example.chitchat_TARGET_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
...
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<meta-data
android:name="com.google.firebase.default_notification_channel_id"
android:value="fcm_default_channel" /> <!-- adding -->
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
...
</manifest>
我不知道我是否为注册添加了一些不同的功能,例如 LifeCycleEvent Listners 和 EmailVerification 是否产生了问题。我也无法记录我不知道为什么的问题。 请,适当的建议。 谢谢
【问题讨论】:
-
当这种情况发生时,在 Background 或 Foreground 期间?
-
背景。基本上我已经限制它只在用户状态在线时接收通知,即当应用程序被最小化或关闭时。在这两种情况下,我都无法打开正确的活动。
-
检查我的答案并告诉我。谢谢
-
我已经做到了,只有我的键值是 user_id 并且我在聊天活动中获得了 user_id
标签: android android-intent firebase-cloud-messaging google-cloud-functions android-notifications