如何保存设备id's推送通知- iOS


How can I save device id's for push notifications - iOS

我有一个php脚本发送通知到一个特定的设备令牌。我通过Xcode中的控制台获得设备令牌但当我的应用发布时,这不是真的。我如何获得用户的设备令牌,将其放在此脚本中以向他们发送推送通知。我听说你可以使用数据库服务器或其他东西,但我是一个初学者,所以你可以提供一个简单的一步一步的答案。由于

我的当前代码:

<?php
// Put your device token here (without spaces):
$deviceToken = 'tokenfordevice1';
// Put your private key's passphrase here:
$passphrase = '*******';
// Put your alert message here:
$message = 'Message';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => ‘default’
    );
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
编辑:我已经设法得到一个SQL服务器在LAMP运行。我现在需要将deviceTokens上传到服务器,但我不确定如何做到这一点。

首先,您必须使用以下函数

AppDelegate获取设备令牌
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}

然后POSTGET此令牌在您的web服务在服务器端。例如,您使用GET,然后使用以下url:-

http://www.yourwebsite.com/push.php?deviceToken=yourdevicetoken&消息=信件内容

你可以将令牌保存在你的NSUserDefaults中,这样你就可以在应用程序的任何地方获得它。

<?php
// Put your device token here (without spaces):
$deviceToken = $_REQUEST['devicetoken'];
// Put your private key's passphrase here:
$passphrase = '*******';
// Put your alert message here:
$message = $_REQUEST['message'];
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => ‘default’
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
   echo 'Message not delivered' . PHP_EOL;
else
   echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);