【问题标题】:PhoneGap PushPlugin - Access iOS APNS Payload?PhoneGap PushPlugin - 访问 iOS APNS 有效负载?
【发布时间】:2014-02-20 19:28:19
【问题描述】:

将 PushPlugin 与我的 PhoneGap 3 iOS 应用程序一起使用,我的推送通知大部分都在工作,但我正在使用自定义负载向 APNS 发送通知。这是它的样子:

$body = array(
    'customID' => $customID
);
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
);

根据我的阅读,这应该由 APNS (scroll down to the bottom) 支持,但 PushPlugin's documentation 根本无法很好地处理 iOS。我想要的是能够在通过点击通知打开我的应用程序时将customID 作为 JS 变量访问。当应用程序只是自行启动时,我不希望发生任何特别的事情。此外,用户点击了哪个通知也很重要——customID 是独特而重要的。我真的很困惑,因为我找不到任何人谈论使用 PushPlugin 处理这些自定义 APNS 有效负载。

【问题讨论】:

    标签: ios cordova push-notification apple-push-notifications phonegap-plugins


    【解决方案1】:

    将自定义变量添加到推送通知负载的正确方法不是将其添加到 aps 命名空间(这是 Apple 保留的),而是单独添加,例如:

    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
    );
    
    $body['payload'] = array(
        'customID' => $customID
    );
    

    在 PhoneGap 中,您可以在通知回调(例如 onNotificationAPN)中读取它,如下所示:

    function onNotificationAPN (e) {
        if (e.customID)
        {
            navigator.notification.alert("CustomID: " + e.customID);
        }
    

    【讨论】:

    • 我在 sn-p 中有一个错字 - 它指的是论点。更正了
    【解决方案2】:

    我从未使用过 PushPlugin,但看看他们的代码,我可以为您指明正确的方向。

    AppDelegate 中,它们将包含通知负载的字典传递给PushPlugin 对象的实例,并调用该实例的notificationReceived 方法。

    PushPlugin 中的notificationReceived 从该字典中构造一个 JSON 字符串,并将其传递给处理通知的 JS 回调方法。

    这是 PushPlugin 文档中的回调方法示例:

    function onNotificationAPN (event) {
        if ( event.alert )
        {
            navigator.notification.alert(event.alert);
        }
    
        if ( event.sound )
        {
            var snd = new Media(event.sound);
            snd.play();
        }
    
        if ( event.badge )
        {
            pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
        }
    }
    

    我的 JavaScript 不是很流利,但 event 应该包含 JSON 字符串以及通知负载的所有数据,包括您的自定义属性。你应该可以在那里访问它。

    【讨论】:

    • 现在,我不得不放弃这个,但我不得不假设这是正确的......
    猜你喜欢
    • 2017-08-23
    • 2013-10-19
    • 1970-01-01
    • 2021-01-13
    • 2016-01-04
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多