Phonegap push notification plugin with iOs

梦想的初衷 提交于 2019-12-10 12:06:07

问题


I'm using phonegap-plugin-push to send push notifications to Android devices.

I'm trying to figure out how to send notification to iOs.

I've registered at https://developers.google.com/mobile/add?platform=ios&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fios%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue

I'get token at client app same way as in Android:

var push = PushNotification.init({
        android: {
            senderID: "5993...475"
        },
        ios: {
            alert: "true",
            badge: true,
            sound: 'false'
        },
        windows: {}
    });

push.on('registration', function(data) {

    $.ajax({
        url: '/save-token/',
        data: {token: data.registrationId},
        success: function (json) {

        }
    });


});

After I get client's token, I try to send'em push notification:

$to = ModelGcmTokens::getInstance()->getToken($userId);

$API_KEY = 'AIzaS...HcEYC346zQ';


        $message = array(
            'data' => array(
                'title' => ($title) ? $title : 'Test!',
                'message' => $message
            ),
            'to' => $to
        );

        $message = json_encode($message);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,"https://gcm-http.googleapis.com/gcm/send");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $message);


        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Authorization:key={$API_KEY}",
            "Content-Type:application/json",
        ));

        // receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec ($ch);

        curl_close ($ch);

        // further processing ....
        if ($server_output == "OK") {

            return true;
        } else {

            return false;
        }

Google cloud messaging responds with InvalidRegistration error:

{"multicast_id":4701637331500989392,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

As I googled this error - it's said that I got client's token wrong. But I get it with excat same script as on Android. And it works on Android.

The only difference, that I see - is format of the token:

Can anyone tell me how do I get push plugin working on iOs? What's wrong with iOs token and how do I supposed to send messages to GCM, to be received by iOs?

Thanks a lot in advance for any advice. I'm really stuck.


回答1:


The way you are calling the init method requests the registration ID from APNS not GCM. If you read the documentation on GCM on iOS at the phonegap-plugin-push repository you will see that init needs to be called like this:

var push = PushNotification.init({
    android: {
        senderID: "5993...475"
    },
    ios: {
        senderID: "5993...475",
        gcmSandbox: true,
        alert: "true",
        badge: true,
        sound: 'false'
    },
    windows: {}
});

If the iOS options has a senderID it will register with GCM.

Also, I notice in the response to one of the comments you don't know the difference between production and development environments on APNS. Well, you really need to read up on this. On iOS devices the APNS environment is different depending if you production and development environment. IF you setup your app as development and you send a push through production it will never arrive at your device.

Now you may be asking why I'm talking about APNS. That's because when you use GCM on iOS you would send your push message to GCM which then will forward your message to the APNS servers and finally to your device.



来源:https://stackoverflow.com/questions/38164686/phonegap-push-notification-plugin-with-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!