【问题标题】:Ionic fcm plugin, onNotification function not working离子 fcm 插件,onNotification 功能不起作用
【发布时间】:2021-02-12 15:28:16
【问题描述】:

我正在尝试实施 FCM 以使用 Firebase 云消息传递向应用用户发送通知。不过,在调试应用程序时,我无法在手机上收到任何通知。

我正在尝试使用https://fcm.googleapis.com/fcm/send 上的邮递员明确发送请求。这是以下请求:

{
    "registration_ids": [
        "my_random_device_ids"
    ],
    "notification": {
        "title": "New request",
        "body": "Your action required to accept.",
        "content_available": false,
        "priority": "high",
        "sound": "default",
        "click_action": "FCM_PLUGIN_ACTIVITY"
    },
    "data": {
        "key": "value"
    }
}

这是我在应用程序中接收通知的代码:

    this.fcm.getToken().then(
        (token) => {
            console.log(token);
        },
        (err) => {
            console.log("FCM", err);
        }
    );
    this.fcm.onNotification().subscribe(data => {
        console.log(data);
    });
    this.fcm.onTokenRefresh().subscribe(async token => {
        var deviceType = this.platform.is('android') ? 'Android' : 'iOS'
        var deviceDetails = { deviceID: this.deviceUUID, deviceToken: token, deviceType: deviceType };
        await this.authService.insertUpdateDeviceToken(deviceDetails);
    });

无论声明this.fcm.onNotification(),我都会在后台收到通知。 此外,当我声明它时,它不会记录任何内容,这意味着它不起作用。有没有办法解决这个问题?

【问题讨论】:

  • 我认为 android.manifest.xml 中存在问题

标签: angularjs ionic-framework firebase-cloud-messaging observable ionic-native


【解决方案1】:

我找到了解决办法。它与此类似,但使用主包而不是 @ionic-native/fcm 包装库。

工作代码如下,在一个基于angular构建的离子项目中实现fcm。

在 cmd[你的项目目录]

ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated@7.3.1

现在,请确保在您的项目中,在插件文件夹中存在一个名为 cordova-plugin-fcm-with-dependecy-updated 的文件夹,并且其中有一个子文件夹 ionic,并且在此子文件夹中还有另一个子文件夹 ngx

现在,您可以在代码中使用它,我在 app.component.ts 中使用它

import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
// Initialise in constructor
.
.
.
.
this.fcm.getToken().then(function(token) {
    console.log(token);
}).catch(err) {
    console.log(err, 'Unable to get token');
});

this.fcm.onNotification().subscribe((data: any) => {
    console.log(data);
});

this.fcm.onTokenRefresh(). subscribe (async token => {
    console.log(token);
});

现在,它可以正常工作了。您可以尝试在收到的令牌上发送通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 2017-09-04
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多