【问题标题】:NativeScript push-plugin not displaying notifications on the notification barNativeScript 推送插件未在通知栏上显示通知
【发布时间】:2018-03-17 07:13:25
【问题描述】:

我正在使用 NativeScript 的push-plugin 接收通知sent from Firebase Cloud Messaging (FCM)

public constructor(
    private _services: PushService,
) {
    const settings = {
        senderID: "XXXXXXXX",
        badge: true,
        clearBadge: true,
        sound: true,
        alert: true,
        interactiveSettings: {
            actions: [{
                identifier: 'READ_IDENTIFIER',
                title: 'Read',
                activationMode: "foreground",
                destructive: false,
                authenticationRequired: true,
            }, {
                identifier: 'CANCEL_IDENTIFIER',
                title: 'Cancel',
                activationMode: "foreground",
                destructive: true,
                authenticationRequired: true,
            }],
            categories: [{
                identifier: 'READ_CATEGORY',
                actionsForDefaultContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER'],
                actionsForMinimalContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER'],
            }],
        },
        notificationCallbackIOS: data => {
            console.log("DATA: " + JSON.stringify(data));
        },
        notificationCallbackAndroid: (message, data, notification) => {
            console.log("MESSAGE: " + JSON.stringify(message));
            console.log("DATA: " + JSON.stringify(data));
            console.log("NOTIFICATION: " + JSON.stringify(notification));

            alert(message);
        },
    };

    PushNotifications.register(settings, data => {
        console.log("REGISTRATION ID: " + data);

        this.toDeviceToken = data;

        PushNotifications.onMessageReceived(settings.notificationCallbackAndroid);
    }, error => {
        console.log(error);
    });
}

使用此代码,当我从 Postman 发送它们时,我可以在 notificationCallbackAndroid 方法中收到成功消息。这是我在日志中得到的:

{
    "multicast_id": 8382252195536318747,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1521270133609562%161a06bff9fd7ecd"
        }
    ]
}

但是,通知栏中没有显示任何通知。

我们必须使用单独的方法来显示通知吗?

【问题讨论】:

  • 我正在尝试通过创建一个新的 IntentService 和 BroadcastReceiver 来实现这一点,如此处所述 - nativescript.org/blog/… 如果您已经实现了相同的功能,请分享信息

标签: firebase push-notification firebase-cloud-messaging nativescript nativescript-angular


【解决方案1】:

补充一点,通知似乎在 iOS 上运行良好。但在 Android 上,它仅在应用程序处于活动状态时才有效。

一些论坛声明推送有效负载需要一个“通知”结构来自动添加到通知托盘,但我查看了提交历史,看起来它已经(尝试)修复了。

无论哪种方式,我都将相同的推送发送 SDK 与另一个应用程序开发 sdk (Xamarin) 一起使用,并且不希望自定义代码在 Nativescript 应用程序上发送不同的有效负载。

我会尝试将 Danzigers 回答为仅限 Android 的代码,看看是否有帮助。如果不是,我将尝试恢复到该插件的旧版本,因为我知道整个问题是回归。

附言。我看到这个插件已经过时了,取而代之的是 firebase 插件,但是当只需要 GCM 和 APS 时,我仍然喜欢这个插件的简单性(而且占用空间更小?)。

更新

安装了本地通知插件,并添加了仅在 Android 上使用的代码,一切都很好。

【讨论】:

    【解决方案2】:

    push-plugin 不会自动创建/显示通知,它只会在收到通知时通知您。

    您需要使用另一个插件 nativescript-local-notifications 来创建/显示它。所以,在你的notificationCallbackAndroidnotificationCallbackIOS 里面你应该有这样的东西:

    LocalNotifications.schedule([{
        title: notificationData.title,
        body: notificationData.body,
    }]);
    

    另外,onMessageReceived 已被弃用,现在你应该只使用notificationCallbackAndroid 和/或notificationCallbackIOS,所以一旦你将push-plugin 更新到最新版本,你就可以去掉这行:

    PushNotifications.onMessageReceived(settings.notificationCallbackAndroid);
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      相关资源
      最近更新 更多