【问题标题】:Push notifications on iOS using phonegap build and no 3rd party service在 iOS 上使用 phonegap 构建推送通知,没有 3rd 方服务
【发布时间】:2014-10-03 11:29:58
【问题描述】:

所以我有这个客户不愿意支付像 PushWoosh 这样的第三方服务来处理推送通知,我需要使用这个插件来实现它们:https://github.com/phonegap-build/PushPlugin on Phonegap Build

这是我目前所拥有的:

应该发送通知的 PHP 文件(在部分教程中找到)

<?php
// Set parameters:
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

// Setup stream:
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

// Open connection:
$apns = stream_socket_client(
    'ssl://' . $apnsHost . ':' . $apnsPort,
    $error,
    $errorString,
    2,
    STREAM_CLIENT_CONNECT,
    $streamContext
);

// Get the device token (fetch from a database for example):
$deviceToken = '...';

// Create the payload:
$message = 'Hallo iOS';
// If message is too long, truncate it to stay within the max payload of 256 bytes.
if (strlen($message) > 125) {
    $message = substr($message, 0, 125) . '...';
}

$payload['aps'] = array('alert' => $message, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

// Send the message:
$apnsMessage
    = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload))
    . $payload;
// fwrite($apns, $apnsMessage);

// Close connection:
@socket_close($apns);
fclose($apns);
?>

我应该在应用程序中添加的 JS 代码(我认为)也在该部分教程中找到:

// Setup push notifications:
try
{
    var pushNotification = window.plugins.pushNotification;
    if (window.device.platform == 'iOS') {
        // Register for IOS:
        pushNotification.register(
            pushSuccessHandler,
            pushErrorHandler, {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb":"onNotificationAPNS"
            }
        );
    }
}
catch(err)
{
    // For this example, we'll fail silently ...
    console.log(err);
}

/**
 * Success handler for when connected to push server
 * @param result
 */
var pushSuccessHandler = function(result)
{
    console.log(result);
};

/**
 * Error handler for when not connected to push server
 * @param error
 */
var pushErrorHandler = function(error)
{
    console.log(error);
};

/**
 * Notification from Apple APNS
 * @param e
 */
var onNotificationAPNS = function(e)
{
    // ...
};

还有一个应该在我将创建的数据库中插入设备令牌的文件。我应该将此文件称为:../deviceAdd.php?token=XXXXXXX

问题是我不知道如何将该设备令牌传递给 deviceAdd 文件。

非常感谢任何帮助!

【问题讨论】:

    标签: php jquery push-notification apple-push-notifications phonegap-build


    【解决方案1】:

    找到了答案。这里是: 我的注册应该是这样的:

    pushNotification.register(
            tokenHandler,
            errorHandler,
            {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb":"onNotificationAPN"
            });
    

    我的 tokenHandler 函数应该是:

    function tokenHandler (result) {
        // Your iOS push server needs to know the token before it can push to this device
        // here is where you might want to send it the token for later use.
        alert('device token = ' + result);
        //insertToken();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多