Search code examples
ioscordovacertificatephonegap-pluginsphonegap-pushplugin

Phonegap plugin push registers device but doesn't receive notifications


So i've been struggling making push plugin (https://github.com/phonegap/phonegap-plugin-push) work on iOS for a while (I don't know anything about iOS development). It seems to register the device correctly, but when i send notifications via pushwatch it sends it without problems to Apple but i don't receive anything (working like a charm for Android).

The fact that it works on Android and i can send the notification to Apple makes me think the problem is with certificates. I've generated the app ID and enabled push notifications, created push certificates for both production and development (neither of them working) and a provisioning profile for development.

On Xcode i've downloaded the provisioning profile from preferences/agent/download all (This is where i think i'm messing this up) and i can build the app without any problem using automatic build settings. When i run the app on my iPhone i get a device token, so i know it registers the device.

Here is my index.js (I'm using the push plugin template app)

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    console.log('Received Device Ready Event');
    console.log('calling setup push');

    if(device.platform == "Android"){ //Android
        console.log('Android');
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler, app.errorHandler, {"senderID":"XXX","ecb":"app.onNotification"});
    }
    else if(device.platform == "iOS"){ //iOS
        console.log('iOS');
        var pushNotification = PushNotification.init({
            "android": {
                "senderID": "XXX"
            },
            "ios": {
              "badge":"true",
              "sound":"true",
              "alert":"true",
              "ecb":"onNotification"
            },
            "windows": {} 
        });
    }

},
// result contains any message sent from the plugin call
successHandler: function(result) {
    console.info('Callback Success! Result = '+result)
},
errorHandler:function(error) {
    console.info(error);
},
onNotification:function(e) {
    push.on('registration', function(data) {
        console.log('registration event: ' + data.registrationId);

        var oldRegId = localStorage.getItem('registrationId');
        if (oldRegId !== data.registrationId) {
            // Save new registration ID
            localStorage.setItem('registrationId', data.registrationId);
            // Post registrationId to your app server as the value has changed
        }
    });

    push.on('error', function(e) {
        console.log("push error = " + e.message);
    });

    push.on('notification', function(data) {
        console.log('notification event');
        navigator.notification.alert(
            data.message,         // message
            null,                 // callback
            data.title,           // title
            'Ok'                  // buttonName
        );
   });
}};

It seems like i can send the notification but apns doesn't know where to send it or like it is sent but my device can't receive it because isn't linked to the certificate.

Thank you in advance.

Edit: Following the steps in this tutorial seems to have solved my problem. https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ The problem i think was the phrase the key asks you to write. Once i've removed it the notifications have started to appear.


Solution

  • Following the steps in this tutorial seems to have solved my problem. https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ The problem i think was the phrase the key asks you to write. Once i've removed it the notifications have started to appear.