【发布时间】:2018-12-04 12:00:24
【问题描述】:
我们能否在 console.log 或 alert 中显示推送通知负载,而不是正常的推送通知。在基于cordova的应用程序中?
【问题讨论】:
标签: firebase cordova push-notification firebase-cloud-messaging
我们能否在 console.log 或 alert 中显示推送通知负载,而不是正常的推送通知。在基于cordova的应用程序中?
【问题讨论】:
标签: firebase cordova push-notification firebase-cloud-messaging
是的,您可以非常轻松地处理 firebase 推送通知,只需按照以下代码 sn-p 即可了解如何处理 firebase 通知负载:
在 MyFirebaseMessagingService:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data Payload: " + remoteMessage.getData().toString());
}
}
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Log.d(TAG, "Notification message: " + message); // It will show notification payload(message) in console log.
}else{
// If the app is in background, firebase itself handles the notification
}
}
如需了解更多信息或 Firebase 通知的工作原理,请关注link
【讨论】:
messaging.setBackgroundMessageHandler 处理程序。 messing.onMessage(function(payload) { console.log('Message received.', payload); // ... });
如果您使用的是 phonegap-push-plugin,那么在“通知”事件监听器中,您可以 console.log() 获取数据。
push.on('notification', function(data) {
console.log(data);
});
【讨论】: