在 Nativescript 中处理推送通知

Handle Push notification in Nativescript

我正在使用 Nativescript 开发实现推送通知的应用程序。假设服务器发送推送通知,并且基于通知有效负载中提到的 action,我将不得不在应用程序中重定向。如果用户点击来自抽屉的通知并且应用程序在后台,则应该执行此重定向。其他情况下,如果应用程序在前台,则不应重定向。我已经为此管理了一个标志,如下所示

app.js

application.on(application.launchEvent, function (args) {
   appSettings.setBoolean('AppForground', true);
});

application.on(application.suspendEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.resumeEvent, function (args) {
   appSettings.setBoolean('AppForground', true);
});

application.on(application.exitEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.lowMemoryEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.uncaughtErrorEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

在推送通知侦听器上

 var settings = {
    // Android settings
    senderID: '1234567890', // Android: Required setting with the sender/project number
    notificationCallbackAndroid: function(data, pushNotificationObject) { // Android: Callback to invoke when a new push is received.
        var payload = JSON.parse(JSON.parse(pushNotificationObject).data);
        if (appSettings.getBoolean('AppForground') == false){
            switch (payload.action) {

                case "APPOINTMENT_DETAIL":
                    frame.topmost().navigate({
                        moduleName: views.appointmentDetails,
                        context: {
                            id: payload.id
                        }
                    });  
                    break;

                case "MESSAGE":
                    frame.topmost().navigate({
                        moduleName: views.appointmentDetails,
                        context: {
                            id: payload.id,
                            from: "messages"
                        }
                    });
                    break;

                case "REFERENCES":
                    frame.topmost().navigate({
                        moduleName: views.clientDetails,
                        context: {
                            id: payload.id,
                            name: ""
                        }
                    });
                    break;

                default: 
            }
        }
    },

    // iOS settings
    badge: true, // Enable setting badge through Push Notification
    sound: true, // Enable playing a sound
    alert: true, // Enable creating a alert

    // Callback to invoke, when a push is received on iOS
    notificationCallbackIOS: function(message) {
        alert(JSON.stringify(message));
    }
};
pushPlugin.register(settings,
    // Success callback
    function(token) {
        // if we're on android device we have the onMessageReceived function to subscribe
        // for push notifications
        if(pushPlugin.onMessageReceived) {
            pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
        }
    },
    // Error Callback
    function(error) {
        alert(error);
    }
);

现在的问题是,如果应用程序处于终止状态并且通知到达。然后它在应用程序启动时将标志设置为 true,这是不应该的。因此,由于不执行重定向,在其他情况下,当应用程序处于前台状态时,它也会在收到通知时浏览页面(不应该)。

我怀疑标志管理是导致问题的原因,但不确定。如果我做的有任何问题,请您指导我好吗?

更新

我正在使用 push-plugin

谢谢。

我用它来通知

https://github.com/EddyVerbruggen/nativescript-plugin-firebase

此插件使用 FCM,它添加到从通知前景参数接收的数据中,因此您可以从有效载荷中确定应用程序是后台(前景==false,应用程序未激活或在通知到达后启动)还是前景(前景==true,应用程序已打开并处于活动状态),但您需要对代码进行一些更改,因为它们是不同的插件

您可以使用 pusher-nativescript npm 模块。

import { Pusher } from 'pusher-nativescript';
/*Observation using the above.
- Project gets build successfully.
- on run -> ERROR TypeError: pusher_nativescript__WEBPACK_IMPORTED_MODULE_6__.Pusher is not a constructor
- Use: import * as Pusher from 'pusher-nativescript';
- Make sure to install nativescript-websocket with this package.
*/
var pusher = new Pusher('Your_app_key', { cluster: 'your_cluster_name' });
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert(JSON.stringify(data));
});