簡體   English   中英

如何保存用於推送通知的設備ID-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 Server在LAMP中運行。 現在,我需要將deviceTokens上傳到服務器中,但是我不確定該怎么做。

首先,您必須使用以下功能從AppDelegate獲取設備令牌

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}

然后POSTGET在服務器端此令牌在您的Web服務。 例如,您使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM