代码之家  ›  专栏  ›  技术社区  ›  Muthukumar Marichamy

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

  •  1
  • Muthukumar Marichamy  · 技术社区  · 6 年前

    我正在使用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);
        });
    }
    

    使用此代码,我可以在中接收成功消息 notificationCallbackAndroid 方法,当我从邮递员那里发送它们时。这是我在日志上看到的:

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

    但是,通知栏中不会显示任何通知。

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

    2 回复  |  直到 6 年前
        1
  •  1
  •   Michael    6 年前

    此外,通知在iOS上似乎工作得很好。但在Android上,它只在应用程序处于活动状态时才起作用。

    一些论坛指出,推送负载需要一个“通知”结构才能自动添加到通知托盘中,但我查看了提交历史记录,它似乎已经(尝试)修复。

    不管怎样,我将同一推送发送SDK与另一个应用程序开发SDK(Xamarin)一起使用,不希望自定义代码在Nativescript应用程序上发送不同的负载。

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

    附言:我看到这个插件已经被淘汰,取而代之的是firebase插件,但我仍然喜欢它的简单性(和更小的占地面积?)当只需要GCM和APS时。

    更新

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

        2
  •  0
  •   Danziger    6 年前

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

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

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

    而且 onMessageReceived 已弃用,现在只应使用 通知回调Android 和/或 通知Callbackios ,所以一旦更新 推送插件 到最新版本,您可以删除以下行:

    PushNotifications.onMessageReceived(settings.notificationCallbackAndroid);