Sending RegistrationID to Push Server (With a UserID)

92 Views Asked by At

I am trying to set up a push notification server, I have gotten the plugin working in my cordova application and I am trying to implement something that sends an ID and the app Registration ID. I am doing this buy placing the user ID within a service and I am trying to store that value within an array along with the regid from the app and send it to my server with a POST request. Here's my code:

push.js

function onNotification(e, $scope, currentUser) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED: ' + e.event + '</li>');
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                $("#app-status-ul").append('<li>REGISTERED -> REGID: ' + e.regid + '</li>');
                $scope.Current = currentUser;
                var userRegistration = [{
                    e.regid
                }, {
                    $scope.Current.currentUser
                }];
                $ajax({
                    data: userRegistration,
                    type: "POST",
                    url: "url/device_register.php",
                    success: function () {
                        alert('Succesfully Registered with the Server.');
                    }
                });
                console.log("RegID = " + e.regid);
            }
            break;

        case 'message':
            //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) {
                $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
                //if the notification contains a soundname, play it.
                // if the notification contains a soundname, play it.
                var my_media = new Media("/android_asset/www/" + e.soundname);
                my_media.play();
            } else {
                //otherwise we were launched because the user touched the 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>');
                }
            }

            $("#app-status-ul").append('<li>MESSAGE -> MSG ' + e.payload.message + '</li>');

            $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>')

            $status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
            break;

        case 'error':
            $("#app-status-ul").append('<li>ERROR -> MSG: ' + e.msg + '</li>');
            break;

        default:
            $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
            break;
    }
}

Everytime I start the app up on my device I get the error:

processMessage failed: Error: TypeError: Cannot set property 'Current' of undefined

I don't understand where I am going wrong. The service is placed within another .js file called app.js and it is being called before push.js and after jQuery. Any help would be very much appreciated.

Update: So after some tinkering it turns out that I am having some issues with calling values from separate .js files. Also in response to the comment on my question, here is the other parts of the code.

app.js

App.controller('LogHomeCtrl', function ($scope, $log, $state, currentUser) {
    var pushNotification;
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        pushNotification = window.plugins.pushNotification;
        $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
        if (device.platform == 'android' || device.platform == 'Android') {

            pushNotification.register(
            successHandler,
            errorHandler, {
                "senderID": "460885134680",
                    "ecb": "onNotification"
            });
        } else {
            pushNotification.register(
            tokenHandler,
            errorHandler, {
                "badge": "true",
                    "sound": "true",
                    "alert": "true",
                    "ecb": "onNotificationAPN"
            });
        }
    }
    function successHandler(result) {
        alert('result = ' + result);
    }
    function errorHandler(error) {
        alert('error = ' + error);
    }
});

App.service('currentUser', function () {
    return {};
});

How would I go about storing the value of the service into push.js?

0

There are 0 best solutions below