簡體   English   中英

使用PHP向IOS設備發送通知

[英]Sending notification using PHP to IOS devices

我有一個應用程序接收使用Objective-c構建在Xcode上的推送通知。 我確實按照Ray Wenderlich本教程的教程 ,通知工作正常,我可以收到即時消息。 我有自己的網站,我想用它來發送使用PHP的通知。 我確實試圖找到關於那個的教程,但我不能。

是否可以使用我的網站發送通知? 我可以使用PHP文件發送通知嗎? 我是否需要將SSl證書更改為生產SSL?

她是我目前使用的PHP代碼:

<?php

$deviceToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';

// Put your alert message here:
$message = 'Hello app';



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

?>

我希望我的問題很清楚,並希望有人給我一些信息或指導我一個可以幫助我的教程。

謝謝

是的,您當然可以使用PHP向iOS設備發送推送通知。 但在發送任何通知之前,您將需要:

1)Apple推送證書(APNs證書) - 這可以從Apple開發人員中心生成。 請注意,證書是每個應用程序,有兩種證書,即開發和分發。 關於如何生成一個鏈接

2)設備的設備令牌 - 這將在您的iOS應用程序上完成。 您在iOS App上的代碼可能看起來像這樣。

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    self.storedDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.storedDeviceToken = [self.storedDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"My token is: %@", self.storedDeviceToken);
}

現在您已擁有推送通知所需的所有工具,您可以從PHP腳本開始。

// Put your device token here (without spaces):
$deviceToken = 'XXXXXXXXXXXXXXX';

$gateway = 'ssl://gateway.push.apple.com:2195';

// Put your private key's passphrase here:
$passphrase = 'YourPassphrase!!';

// Put your alert message here:
$message = 'Yoooo! What\'s up man!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PathToGeneratedCertificateOnStep1');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    $gateway, $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);

有關Apple推送通知訪問支持的有效負載的更多詳細信息。

筆記:

  • 如果您手動安裝iOS應用程序以進行測試,則設備令牌僅對沙盒通知有效。 所以,你的$gateway將是'ssl://gateway.sandbox.push.apple.com:2195'

  • 無法在模擬器上接收通知。

暫無
暫無

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

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