简体   繁体   中英

cordova push + angular - implement logic on when notification recieved

I am using cordova push notification plugin un my angular application. I have created this service for the notifications:

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;
});

Now, I am able to register (I am getting the registration ID) but when the device receives a notification it seems that the application not running the js logic written in the callback function under the message case: test(e.payload) .

Note that I'm am receiving the notification butlocalStorageService.set('notification_url', notificationData.url); is never being set to the provided value.

Can you please help me to understand why it is not running? is it even suppose to run (if the application is not running - installed but not running)

The thing is that I made the registration only on login. Now I have added this line to my module run callback:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM