Phonegap推送通知插件与iOs [英] Phonegap push notification plugin with iOs

查看:246
本文介绍了Phonegap推送通知插件与iOs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用phonegap-plugin-push将推送通知发送到Android设备。



我想知道如何向iOs发送通知。 / p>

我已在



任何人都可以告诉我如何让推送插件在iOs上工作? iOs令牌有什么问题,我应该如何向GCM发送消息,以便由iOs接收?



感谢您提供任何建议。我真的卡住了。

解决方案

调用init方法的方法向APNS请求注册ID,而不是GCM。如果您阅读了有关 GCM的文档在iOS上在phonegap-plugin-push仓库你会看到init需要被这样调用:

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

如果iOS选项有 senderID 将注册GCM。



此外,我注意到您对其中一个意见的回复,您不知道生产 开发环境。嗯,你真的需要阅读这个。在iOS设备上,APNS环境是不同的,取决于生产开发环境。如果您将应用程序设置为开发,并通过生产发送推送,它将永远不会到达您的设备。 p>

现在你可能会问为什么我在说APNS。这是因为当您在iOS上使用GCM时,您会将您的推送消息发送到GCM,然后它会将您的邮件转发到APNS服务器,最后转发到您的设备。


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.

解决方案

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.

这篇关于Phonegap推送通知插件与iOs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆