如何保存推送通知的设备ID - iOS

时间:2014-06-08 04:40:00

标签: php ios sql apple-push-notifications

我有一个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);
编辑:我已经设法在LAMP中运行一个SQL服务器。我现在需要将deviceTokens上传到服务器,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

首先,您必须使用以下功能

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

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

http://www.yourwebsite.com/push.php?deviceToken=yourdevicetoken&message=yourmessage

您可以将令牌保存在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);