gpt4 book ai didi

android - FCM 通知标题仍然是 "FCM Message"

转载 作者:太空狗 更新时间:2023-10-29 16:12:43 26 4
gpt4 key购买 nike

我正在尝试使用 Firebase 云消息传递。我将通知从 Node.js 服务器发送到我注册到通知系统的应用程序。

我的问题是,在 Android 5.1 上,即使我在 nofitification json 中设置了 title 属性,通知也是“FCM 消息”。它在 Android 6.0 中运行良好。我也尝试重新启动我的设备。

enter image description here

这是我用来发送通知的代码:

function sendNotificationToUser(userToken, message, onSuccess) {
request({
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: {
'Content-Type' :' application/json',
'Authorization': 'key='+API_KEY
},
body: JSON.stringify({
notification: {
"title": 'My App Name',
"body": message,
"sound": 'default'
},
to : userToken
})
}, function(error, response, body) {
if (error) { console.error(error); }
else if (response.statusCode >= 400) {
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
}
else {
onSuccess();
}
});
}

如您所见,我发送的通知标题是“我的应用程序名称”,但在设备上它显示“FCM 消息”。

我必须做什么?!

最佳答案

您需要传递标题然后在 remoteMessage.getNotification().getTitle() 中接收它,这将捕获标题然后显示在顶部或从网络传递完整的 JSON 并接收类似这个

JSONObject jsonObject = new JSONObject(remoteMessage.getData());

完整的方法如下:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background
Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}

Ref link

关于android - FCM 通知标题仍然是 "FCM Message",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40383340/

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