gpt4 book ai didi

ios - 如何为开发设置推送通知?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:34 30 4
gpt4 key购买 nike

我已经为 ios 开发了一个应用程序,它已经在应用程序商店上发布了。推送通知系统在开发中运行良好,但现在,我根本没有收到任何通知。在发布之前,我已经生成了与启用了推送并配置了生产推送 SSL 证书的 App Id 关联的开发配置文件。我已经下载了 Production Push SSL 证书并将其安装在我的钥匙串(keychain)访问中,导出它及其私钥,将它们转换为 pem 并将它们合并到一个唯一的 pem 文件中,我将其上传到我的服务器,其中包含发送通知。我已将我的 php 脚本中的服务器更改为生产服务器之一 (ssl://gateway.push.apple.com:2195)。脚本似乎仍然发送通知并且没有触发任何错误。

我错过了什么?

这是我的 php 脚本:

<?PHP
/* Here there's Code to retrieve device tokens and put it in a variable $deviceToken from my online database and then...*/


$message = "Message to be sent";
$payload = '{
"aps" :

{ "alert" : "'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'secret');

// se l'app è già sull'appstore togliere sandbox e lasciare solo gateway.push.apple.com:2195
// invece di gateway.sandbox.push.apple.com:2195
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
print "Failed to connect $err $errstrn";
return;
} else {
print "Notifications sent! </br>";
}

foreach($devArray as $deviceToken){
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "n to device:".$deviceToken."</br>";
fwrite($fp, $msg);
}
fclose($fp);

?>

最佳答案

因为您必须使用有效的“生产推送 SSL 证书”而不是“开发推送 SSL 证书”重新生成 PEM。

编辑:抱歉,我读错了行两次。程序看起来不错,我贴出我一直使用的php代码,看起来很相似,但你可以尝试编辑它。

<?php
// Passphrase for the private key (ck.pem file)
// $pass = ”;
// Get the parameters from http get or from command line
$message = 'Testo Notifica Push';
$badge = 1;
$sound = 'default';

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

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);

if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;

/* End of Configurable Items */

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

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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;

// Construct the notification payload
$body = array();
$body['aps'] = array(’alert’ => $message);

if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;

// 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);
?>

关于ios - 如何为开发设置推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15477104/

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