NativeScript 推送插件不在通知栏上显示通知

NativeScript push-plugin not displaying notifications on the notification bar

我正在使用 NativeScript 的 push-plugin to receive notifications 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"
        }
    ]
}

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

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

push-plugin不会自动create/display通知,它只会在收到通知时通知您。

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

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

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

PushNotifications.onMessageReceived(settings.notificationCallbackAndroid);

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

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

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

我将尝试 Danzigers 的答案作为 Android-only 代码,看看它是否有帮助。如果不是,我会尝试恢复到该插件的旧版本,据我所知,整个问题都是回归。

PS。我看到这个插件已经被 firebase 插件取代,但我仍然喜欢这个插件的简单性(和更小的占用空间?),只需要 GCM 和 APS.

更新

已安装本地通知插件,并添加了仅在 Android 上使用它的代码,一切正常。