带有iOs的Phonegap推送通知插件

时间:2016-07-02 21:36:17

标签: ios iphone cordova phonegap-plugins phonegap-pushplugin

我正在使用phonegap-plugin-push向Android设备发送推送通知。

我正在试图找出如何向iOs发送通知。

我已在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

注册

我在客户端应用程序中使用与在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) {

        }
    });


});

获得客户端令牌后,我尝试发送'em推送通知:

$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云消息传递以 InvalidRegistration 错误响应:

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

当我搜索这个错误时 - 据说我的客户端令牌有误。 但是我用与Android相同的脚本来获取它。它适用于Android。

我看到的唯一区别是令牌的格式:

enter image description here

有谁能告诉我如何让推送插件在iOs上运行? iOs令牌有什么问题,我该如何向GCM发送消息,以便被iOs接收?

非常感谢您提出任何建议。我真的被卡住了。

1 个答案:

答案 0 :(得分:3)

您调用init方法的方式是从APNS而不是GCM请求注册ID。如果您在phonegap-plugin-push存储库中阅读GCM on iOS上的文档,您将看到需要像这样调用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注册。

此外,我在回复其中一条评论时注意到您不知道APNS上productiondevelopment环境之间的区别。好吧,你真的需要阅读这个。在iOS设备上,APNS环境会有所不同,具体取决于您是productiondevelopment环境。如果您将应用设置为development并且通过production发送推送,那么它将永远不会到达您的设备。

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

相关问题