ios - 带有 iO 的 Phonegap 推送通知插件

标签 ios iphone cordova phonegap-plugins phonegap-pushplugin

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

我正在尝试弄清楚如何向 iO 发送通知。

我已经在 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 注册了

我在客户端应用程序中获取 token 的方式与在 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) {

        }
    });


});

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

$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"}]}

当我用谷歌搜索这个错误时 - 据说我弄错了客户的 token 。 但是我使用与 Android 上相同的脚本来获取它。它适用于 Android。

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

enter image description here

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

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

最佳答案

您调用 init 方法的方式是从 APNS 而不是 GCM 请求注册 ID。如果您阅读关于 GCM on 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 注册。

此外,我在对其中一条评论的回复中注意到,您不知道 APNS 上的productiondevelopment 环境之间的区别。嗯,你真的需要仔细阅读这个。在 iOS 设备上,APNS 环境是不同的,具体取决于您的productiondevelopment 环境。如果您将您的应用设置为开发并通过生产发送推送,它永远不会到达您的设备。

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

关于ios - 带有 iO 的 Phonegap 推送通知插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38164686/

相关文章:

iphone - WEPopoverController PresentPopoverFromBarButtonItem 导致指定的 UIBarButtonItem 消失

ios - 当应用程序关闭时,在内存中保留一个 Firebase 监听器

ios - 在 iOS 13 中覆盖 traitCollection

ios - UIScrollView初始偏移量

cordova - 如何在OnsenUI中增加popPage()的堆栈?

javascript - 全新项目的 Android 中不会显示 Ionic 启动画面

javascript - 清除phonegap中的cookie/禁用phonegap使用cookie

iOS - 当应用程序被用户强制退出时处理静默推送通知

iphone - 如何通过使用 NSPredicate 和 NSDate 选择最近 3 个月

ios - UICollectionView - 将 Storyboard 项目移植到无 IB 项目