Push-уведомления на iOS с использованием PhoneGap и без сторонних сервисов

Поэтому у меня есть клиент, который не готов платить за сторонние сервисы, такие как PushWoosh, для обработки push-уведомлений, и мне нужно реализовать их с помощью этого плагина: 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

Решение

Нашел ответ. Вот:
Мой регистр должен быть таким:

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

}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector