使用phonegap构建在iOS上推送通知,没有第三方服务

时间:2014-10-03 11:29:58

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

所以我有这个客户不愿意支付像PushWoosh这样的第三方服务来处理推送通知,我需要使用这个插件来实现它们:https://github.com/phonegap-build/PushPlugin在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文件。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

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

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();

}