gpt4 book ai didi

php - 使用 PHP 向 IOS 设备发送通知

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:33 25 4
gpt4 key购买 nike

我有一个应用程序可以接收推送通知,它使用 Objective-c 在 Xcode 上构建。我确实遵循了本教程 this tutorial 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 Push Certificate (APNs Certificate) - 这可以从 Apple Developer Center 生成。请注意,证书是针对每个应用程序的,并且有两种证书,即开发证书和分发证书。关注This Link关于如何生成一个。

2) 您设备的设备 token - 这将在您的 iOS 应用程序上完成。您在 iOS 应用程序上编写代码可能看起来像这样。

- (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 推送通知支持的有效负载的更多详细信息 visit .

注意事项:

  • If you are installing the iOS app manually for testing then the device token is only valid for sandbox notification. So, your $gateway will be 'ssl://gateway.sandbox.push.apple.com:2195'

  • Notification cannot be received on simulator.

关于php - 使用 PHP 向 IOS 设备发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com