【问题标题】:How to get FCM Intent Extras in IONIC如何在 IONIC 中获得 FCM Intent Extras
【发布时间】:2021-02-18 14:50:09
【问题描述】:

我正在处理 IONIC 和 FCM 的问题。如下所示,我正在从 Node JS 中的服务器向 IONIC 中的应用程序发送“数据”而不是“通知”,我在控制台中看到数据已发送,但我不知道如何访问它,根据对于文档,它在 Intent extras 中,但我没有成功。我可以使用也为我服务的“通知”,但问题在于,只有当您单击推送通知时,您才能获得数据……因此,如果客户端没有单击通知并从后台将无法获取该数据...

我在 Node 中的服务器:

const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24,
      };
    
      const message_notification = {
        /*
        notification: {
          title: "¡Tienes un nuevo pedido!",
          body: "Tienes 30 segundos para aceptar el pedido, de lo contrario será rechazado.",
          sound: "default",
          vibrate: 1,
          Badge: 1,
          click_action: "FCM_PLUGIN_ACTIVITY"
        },
        */
        data: {
          detalles:  JSON.stringify(nuevopedido)
        },

      };
    
      admin
        .messaging()
        .sendToDevice(primeroCola.socket_id, message_notification, options)
        .then((response) => {
          console.log("Notificación enviada", response);
          
        })
        .catch((error) => {
          console.log(error);
        });

我在 ionic 中的应用:

 this.fcm.onNotification().subscribe((data) => {

            console.log(JSON.parse(data.detalles))

        });

我在 Ionic 中尝试了 Cordova Intent Web 插件,但我无法获得 FCM Intent,只是我在 Ionic 中的项目,我对 Android 不太了解。

WebIntent.getIntent().then((intent) => {
            console.log('Action' + JSON.stringify(intent.action));
            const intentExtras = intent.extras;
            if (intentExtras == null) {
                console.log("No hay extras")
            } else {
                console.log('Extras de intent: ' + JSON.stringify(intentExtras));
            }
        }, (error) => {
            console.log('Error getting launch intent', error);
        });

【问题讨论】:

    标签: android ionic-framework firebase-cloud-messaging


    【解决方案1】:

    onMessageReceived 中添加 FCMPlugin.sendPushPayload(data); 之前(文件:MyFirebaseMessagingService):

    FCMPlugin.setInitialPushPayload(data);
    

    当你在 FCM 中只发送“数据”而没有“通知”时,它不会做一组有效载荷(FCMPlugin.setInitialPushPayload(data)),因为没有什么可以点击,没有通知,它无法监听到“通知”事件,因为应用程序已关闭,所以当您再次打开应用程序时(当应用程序关闭时)以某种方式获取数据,然后设置在您的应用程序中使用 getInitialPushPayload 时,您会得到数据:

    离子应用:

    async created() {
            
            const payload = await this.fcm.getInitialPushPayload()
            console.log(payload)
        },
    

    现在,当您打开已关闭的应用程序时,您将收到数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多