【问题标题】:Handle Push notification in Nativescript在 Nativescript 中处理推送通知
【发布时间】:2016-09-16 06:02:25
【问题描述】:

我正在开发实现推送通知的 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

谢谢。

【问题讨论】:

  • 我会检查到达设备后的数据是否没有后台参数,然后您将能够确定应用程序是后台还是前台
  • payload没有后台参数,在app.js本地管理。基于此,我必须管理导航。

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


【解决方案1】:

我用它来通知

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

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

【讨论】:

  • 我在浏览器内工作。我如何管理 gradles 和库?
  • 即使在服务器端,我也必须对发送推送进行更改。你能提供一些演示/链接吗?
  • 我没有问题发送服务器端代码的演示,但如果您在那里构建整个应用程序或来自“函数(数据, pushNotificationObject)" 来自 android 回调?
  • { google.sent_time=1474027549237, smallIcon=small_icon, data={ "title": "New Appointment", "body": "John Smith 要求在 6 月 23 日星期五 9 点进行预约: 00PM(1 小时 Incall)”,“id”:2,“action”:“APPOINTMENT_DETAIL”},sound=1,title=NewAppointment,vibrate=1,google.message_id=0:1474027549250154%d8dc7159f9fd7ecd,largeIcon=large_icon, message=JohnSmithisrequestinganappointmentonFriday, June23at9: 0 0PM(1-hourIncall), collapse_key=do_not_collapse }
  • 你可以使用notification { custom_tag:"data"}和ui/frame topmost().navigate({...})
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2019-06-19
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多