我正在尝试为我的应用设置推送通知。
到目前为止,当应用程序通过Xcode安装时(基本上处于开发模式),我可以成功接收推送通知。但是,只要我从TestFlight安装应用程序并尝试使用新设备令牌,APN就会以BadDeviceToken
回答。
我做了自己的研究,但所有关于此的问题似乎已经过时了:虽然这些使用了* .pem和* .p12证书,但我使用* .p8证书。据我所知,p8证书适用于开发和生产,所以我不会在这里看到问题?
我使用Github的edamov\pushok
库使用以下代码:
<?php
require '../../vendor/autoload.php';
use Pushok\AuthProvider;
use Pushok\Client;
use Pushok\Notification;
use Pushok\Payload;
use Pushok\Payload\Alert;
$options = [
'key_id' => 'xxx', // The Key ID obtained from Apple developer account
'team_id' => 'xxx', // The Team ID obtained from Apple developer account
'app_bundle_id' => 'xxx', // The bundle ID for app obtained from Apple developer account
'private_key_path' => 'xxx (p8 certificate path)', // Path to private key
'private_key_secret' => null // Private key secret
];
$authProvider = AuthProvider\Token::create($options);
$alert = Alert::create()->setTitle('Hello!');
$alert = $alert->setBody('First push notification');
$payload = Payload::create()->setAlert($alert);
//set notification sound to default
$payload->setSound('default');
//add custom value to your notification, needs to be customized
$payload->setCustomValue('key', 'value');
$deviceTokens = ['xxx (device token from TestFlight installation)'];
$notifications = [];
foreach ($deviceTokens as $deviceToken) {
$notifications[] = new Notification($payload,$deviceToken);
}
$client = new Client($authProvider, $production = false);
$client->addNotifications($notifications);
$responses = $client->push(); // returns an array of ApnsResponseInterface (one Response per Notification)
foreach ($responses as $response) {
echo($response->getApnsId());
echo($response->getStatusCode());
echo($response->getReasonPhrase());
echo($response->getErrorReason());
echo($response->getErrorDescription());
}
那么如何在生产模式下使用p8证书设置APN?我是否需要创建生产证书并以某种方式将其包含在某个地方?
答案 0 :(得分:0)
尝试发送推送时,如果通过Testflight安装了应用,您是否保持启用沙盒?