在console.log中显示Firebase推送通知或警报?

时间:2018-12-04 12:00:25

标签: firebase cordova push-notification firebase-cloud-messaging

我们是否可以在console.log或警报中显示推送通知有效负载,而不是正常的推送通知。在基于Cordova的应用程序中?

2 个答案:

答案 0 :(得分:2)

是的,您可以很轻松地处理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 Notification的工作方式,请遵循以下link

答案 1 :(得分:0)

如果您正在使用 phonegap-push-plugin ,则可以在“通知”事件监听器中对数据进行console.log()。

 push.on('notification', function(data) {
   console.log(data);
  });