【问题标题】:Sending notification using PHP to IOS devices使用 PHP 向 IOS 设备发送通知
【发布时间】:2015-04-28 19:03:46
【问题描述】:

我有一个应用程序接收推送通知,它使用 Objective-c 在 Xcode 上构建。我确实遵循了 Ray Wenderlich 的本教程 this tutorial,通知工作正常,我可以立即收到消息。我有自己的网站,我想用它来使用 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);

?>

我希望我的问题很清楚,并希望有人给我一些信息或指导我学习可以帮助我的教程。

谢谢

【问题讨论】:

  • 是的,您可以在您的网站上使用相同的页面。您需要为生产环境创建另一个证书,您将使用该证书部署您的 APP 进行存储。您还需要将 URL 从 ssl://gateway.sandbox.push.apple.com:2195 更改为 ssl://gateway.push.apple.com:2195
  • 这很简单,但我需要删除开发的ssl r也没关系。
  • 开发证书不需要删除。您仍然可以将它用于您的开发构建应用程序。
  • 从我的网站运行 PHP 后出现此消息:连接失败:110 连接超时
  • 你认为是不是因为该应用尚未在 Apple Store 上架,所以我有这个错误

标签: php ios xcode


【解决方案1】:

是的,您当然可以使用 PHP 向 iOS 设备发送推送通知。但在发送任何通知之前,您需要:

1) Apple Push Certificate (APNs Certificate) - 这可以从 Apple Developer Center 生成。请注意,证书是每个应用程序的,有两种证书,开发和分发。关注This Link 了解如何生成。

2) 您设备的设备令牌 - 这将在您的 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 Push Notification visit 支持的有效负载的更多详细信息。

注意事项:

  • 如果您手动安装 iOS 应用程序进行测试,那么设备 令牌仅对沙盒通知有效。所以,你的$gateway 将 是'ssl://gateway.sandbox.push.apple.com:2195'

  • 模拟器无法收到通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多