1

For a while now, I have been trying to figure out how to send push notifications. The app I have made is for Android right now, but I want to extend it to other devices once I figure this out. I've looked into various services, such as Amazon SNS, but they all neglect to include how to get the device token. They all assume you know how to do that.

So what I am asking is: how do I get a device token/registration ID for a device?

I tried using this code:

var tokenID = "";

document.addEventListener("deviceready", function(){
    //Unregister the previous token because it might have become invalid. Unregister everytime app is started.
    window.plugins.pushNotification.unregister(successHandler, errorHandler);

    if(intel.xdk.device.platform == "Android")
    {
        //register the user and get token
        window.plugins.pushNotification.register(
        successHandler,
        errorHandler,
        {
            //senderID is the project ID
            "senderID":"",
            //callback function that is executed when phone recieves a notification for this app
            "ecb":"onNotification"
        });
    } 
    else if(intel.xdk.device.platform == "iOS") 
    {
        //register the user and get token
        window.plugins.pushNotification.register(
        tokenHandler,
        errorHandler,
        {
            //allow application to change badge number
            "badge":"true",
            //allow application to play notification sound
            "sound":"true",
            //register callback
            "alert":"true",
            //callback function name
            "ecb":"onNotificationAPN"
        });
    }
}, false);

//app given permission to receive and display push messages in Android.
function successHandler (result) {
    alert('result = ' + result);
}

//app denied permission to receive and display push messages in Android.
function errorHandler (error) {
    alert('error = ' + error);
}

//App given permission to receive and display push messages in iOS
function tokenHandler (result) {
    // Your iOS push server needs to know the token before it can push to this device
    // here is where you might want to send the token to your server along with user credentials.
    alert('device token = ' + result);
    tokenID = result;
}

//fired when token is generated, message is received or an error occured.
function onNotification(e) 
{
    switch( e.event )
    {
        //app is registered to receive notification
        case 'registered':
            if(e.regid.length > 0)
            {
                // Your Android push server needs to know the token before it can push to this device
                // here is where you might want to send the token to your server along with user credentials.
                alert('registration id = '+e.regid);
                tokenID = e.regid;
            }
            break;

        case 'message':
          //Do something with the push message. This function is fired when push message is received or if user clicks on the tile.
          alert('message = '+e.message+' msgcnt = '+e.msgcnt);
        break;

        case 'error':
          alert('GCM error = '+e.msg);
        break;

        default:
          alert('An unknown GCM event has occurred');
          break;
    }
}

//callback fired when notification received in iOS
function onNotificationAPN (event) 
{
    if ( event.alert )
    {
        //do something with the push message. This function is fired when push message is received or if user clicks on the tile.
        alert(event.alert);
    }

    if ( event.sound )
    {
        //play notification sound. Ignore when app is in foreground.
        var snd = new Media(event.sound);
        snd.play();
    }

    if ( event.badge )
    {
        //change app icon badge number. If app is in foreground ignore it.
        window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
    }
}

All I get is an alert that says "result = ok". The alerts later on in the code don't happen. I've tried making sense of the code but I'm not getting anywhere. Any suggestions? Is there a tutorial for this I'm not finding?

galath
  • 5,717
  • 10
  • 29
  • 41

2 Answers2

0

Those legacy intel.xdk functions are being retired (the will continue to live in an 01.org, see the notice on this page: https://software.intel.com/en-us/node/492826).

I recommend you investigate one of the many push notification Cordova plugins that are available. Use your favorite web search tool to search for something like "cordova phonegap push notification plugin" to find some. The good ones will have examples of how to use.

xmnboy
  • 2,314
  • 2
  • 14
  • 31
  • I was using a Cordova plugin: https://github.com/phonegap-build/PushPlugin Is that no longer any good? – Chris Buckley Jul 16 '15 at 16:23
  • 1
    Sorry, I looked too quickly at your code and saw the references to intel.xdk.* and assumed you were using the AppMobi push notification stuff... Regarding the plugin you reference, I cannot tell you if it is "good" or not, we're not able to track all the plugins that are out there. Notice that he's got multiple release tags, so you can try older versions. See the "branch: master" pulldown and select the "tags" -- then reference the plugin like this: https://github.com/phonegap-build/PushPlugin.git#2.3.1 to get the version tagged with the 2.3.1 tag. – xmnboy Jul 16 '15 at 17:54
0

Note:-

Result OK means that the plugin is installed correctly and has run properly.


Problems could be due to:

  • Incorrect sender ID
  • Testing in emulator without adequate setup

Important - Push notifications are intended for real devices. They are not tested for WP8 Emulator. The registration process will fail on the iOS simulator. Notifications can be made to work on the Android Emulator, however doing so requires installation of some helper libraries, as outlined here, under the section titled "Installing helper libraries and setting up the Emulator".

  • onNotification must be available as a global object. So try attaching it to the window. Refer to this question

Examples of properly initializing PushPlugin in:

Community
  • 1
  • 1
Ajoy
  • 1,838
  • 3
  • 30
  • 57