我们是否可以在console.log或alert中显示推送通知负载,而不是正常的推送通知。在基于cordova的应用中?
是的,你可以很容易地处理firebase推送通知,只需按照下面的代码片段,你就可以理解如何处理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通知的工作原理,请点击此链接
如果您使用phonegap推送插件,那么在"notification"事件侦听器中,您可以console.log((数据。
push.on('notification', function(data) {
console.log(data);
});