Jac*_*nti 4 android firebase firebase-cloud-messaging
我正在尝试使用 Firebase 云消息传递。我将通知从 Node.js 服务器发送到我注册到通知系统的应用程序。
我的问题是,在 Android 5.1 上,即使我在 nofitification json 中设置了标题属性,通知也是“FCM 消息”。它在 Android 6.0 中运行良好。我也尝试重新启动我的设备。
这是我用来发送通知的代码:
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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我发送的通知标题是“我的应用程序名称”,但在设备上显示“FCM 消息”。
我需要做什么?!
小智 6
您需要传递标题然后接收它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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9690 次 |
| 最近记录: |