iOs 的 Phonegap 推送通知插件

Phonegap push notification plugin with iOs

我正在使用 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) {

        }
    });


});

获得客户的令牌后,我尝试向他们发送推送通知:

$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.

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

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

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

您调用 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 服务器,最后转发到您的设备。