cordova push + angular - 在收到通知时实现逻辑

时间:2014-11-29 17:30:25

标签: javascript android angularjs cordova

我正在使用cordova推送通知插件而非我的角度应用程序。我已经为通知创建了这项服务:

define(["app"], function (app) {
    'use strict';

    var pushNotificationService = function ($q, localStorageService) {
        var registrationDefered;

        function registrationSuccess(result) {

        }

        function registrationError(error) {

        }

        function test(notificationData) {
            localStorageService.set('notification_url', notificationData.url);
        }

        function isOnBrowser() {
            var result = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
            return !result;
        }

        //Android Amazon callback
        app.onNotificationGCM = function(e) {
            console.log(e);
            switch(e.event) {
                case 'registered':
                    if (e.regid.length > 0) {

                        // Your GCM push server needs to know the regID before it can push to this device
                        // here is where you might want to send it the regID for later use.
                        var data = {
                            registrationID: e.regid,
                            device: device.platform.toLocaleLowerCase()
                        };
                        registrationDefered.resolve(data);
                    }
                    break;

                case 'message':

                    test(e.payload);
                    // if this flag is set, this notification happened while we were in the foreground.
                    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                    if (e.foreground) {
                        console.log(e);
                        // on Android soundname is outside the payload.
                        // On Amazon FireOS all custom attributes are contained within payload
                        var soundfile = e.soundname || e.payload.sound;

                        // if the notification contains a soundname, play it.
                        var my_media = new Media("/android_asset/www/"+ soundfile);
                        my_media.play();
                    } else {

                        // otherwise we were launched because the user touched a notification in the notification tray.
                        if (e.coldstart) {
                            //$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                        } else {
                            //$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                        }
                    }

                    break;

                case 'error':
                    break;

                default:
                    break;
            }
        };

        //public
        var serviceApi = {
            register: function() {
                var pushNotification = window.plugins.pushNotification;
                registrationDefered = $q.defer();

                debugger;
                if(device.platform.toLocaleLowerCase() == 'android' || device.platform == "amazon-fireos") {
                    pushNotification.register(
                        registrationSuccess,
                        registrationError,
                        {
                            "senderID": "238062858076",
                            "ecb":"angular.module('App').onNotificationGCM"
                        }
                    );
                } else if(isOnBrowser()) {
                    var data = {
                        registrationId: "",
                        device: "browser"
                    };

                    registrationDefered.resolve(data);
                }



                /* else if (device.platform == 'blackberry10'){
                    pushNotification.register(
                        registrationSuccess,
                        registrationError,
                        {
                            invokeTargetId : "replace_with_invoke_target_id",
                            appId: "replace_with_app_id",
                            ppgUrl:"replace_with_ppg_url", //remove for BES pushes
                            ecb: "pushNotificationHandler",
                            simChangeCallback: replace_with_simChange_callback,
                            pushTransportReadyCallback: replace_with_pushTransportReady_callback,
                            launchApplicationOnPush: true
                        });
                } else {
                 pushNotification.register(
                 tokenHandler,
                 errorHandler,
                 {
                 "badge":"true",
                 "sound":"true",
                 "alert":"true",
                 "ecb":"onNotificationAPN"
                 });
                 }*/

                return registrationDefered.promise;
            }
        };

        //public
        return {
            register:    serviceApi.register
        }
    };

    return pushNotificationService;
});

现在,我可以注册(我正在获取注册ID),但是当设备收到通知时,似乎应用程序没有运行在回调函数中编写的js逻辑,在消息情况下:test(e.payload)

请注意,我收到的通知butlocalStorageService.set('notification_url', notificationData.url);永远不会被设置为提供的值。

你能帮我理解它为什么没有运行吗?是甚至假设运行(如果应用程序没有运行 - 已安装但未运行)

1 个答案:

答案 0 :(得分:0)

事情是我只在登录时进行了注册。现在我已将此行添加到我的模块运行回调:

app.run(function() {
   pushNotificationService.register().then(function(result) {
            console.log(result); //this just for debugging 
   });
});