【问题标题】:Show firebase push notification in console.log or alert?在 console.log 或警报中显示 firebase 推送通知?
【发布时间】:2018-12-04 12:00:24
【问题描述】:

我们能否在 console.log 或 alert 中显示推送通知负载,而不是正常的推送通知。在基于cordova的应用程序中?

【问题讨论】:

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


    【解决方案1】:

    是的,您可以非常轻松地处理 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

    【讨论】:

    • 如果你想在警报中显示有效载荷,只需在 android 中使用警报对话框:stackoverflow.com/questions/2115758/…
    • // 处理传入的消息。在以下情况下调用: // - 在应用获得焦点时收到消息 // - 用户单击由服务工作者创建的应用通知 // messaging.setBackgroundMessageHandler 处理程序。 messing.onMessage(function(payload) { console.log('Message received.', payload); // ... });
    【解决方案2】:

    如果您使用的是 phonegap-push-plugin,那么在“通知”事件监听器中,您可以 console.log() 获取数据。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多