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


Push notifications on iOS using phonegap build and no 3rd party service

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

以下是我目前所拥有的:

一个应该发送通知的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)
{
    // ...
};

以及一个文件,该文件应该在我将创建的数据库中插入设备令牌。我应该将此文件称为:/设备添加php?令牌=XXXXXXX

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

非常感谢您的帮助!

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

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