处理Nativescript中的推送通知

时间:2016-09-16 06:02:26

标签: javascript android push-notification nativescript telerik-appbuilder

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

感谢。

2 个答案:

答案 0 :(得分:1)

我将此用作通知

  

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

此插件使用FCM,它添加到从通知前景参数接收的数据,因此从有效负载,您可以确定应用程序是否为后台(前景==假,应用程序未激活或在通知到达后启动)或前景(前景==是的,应用程序是开放且活跃的),但您需要对代码进行一些更改,因为它们是不同的插件

答案 1 :(得分:0)

您可以使用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));
});