swift - 有没有办法通过Push Kit接收通知?

标签 swift pushkit

当应用程序在 iOS 中被终止时,我希望收到静默推送通知。 所以我在我的应用程序中实现了 Push Kit 框架。有没有办法通过push Kit发送通知?作为 FCM 控制台或推送尝试....

最佳答案

FCM 不支持静默推送通知。

Does Firebase Cloud Messaging support VOIP pushkit services?

您可以尝试使用下面的 php 文件和终端命令。

<小时/>

PHP 文件

<?php

// Put your device token here (without spaces):


$deviceToken = '1234567890123456789';
//


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

// Put your alert message here:
$message = 'My first silent push notification!';



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.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,
'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(
'content-available'=> 1,
'alert' => $message,
'sound' => 'default',
'badge' => 0,
);



// 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);
<小时/>

使用下面的命令创建 pem 文件并在上面的代码中使用它

$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem

// Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert.  
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying - Enter PEM pass phrase:

// To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings.
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem

Enter pass phrase for PushChatKey1.pem:

writing RSA key

// To join the two .pem file into one file:
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem

Refer more information

关于swift - 有没有办法通过Push Kit接收通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061489/

相关文章:

arrays - 使用结构体存储动态数据

swift - 将 NSData 的每个字节转换为等效的 Int 值

ios - 从 SKScene 获取 UIViewController

ios - 如何在非根 Viewcontroller 的 ViewController 上创建具有左右按钮的导航栏?

uinavigationbar - 在 iOS8 Swift 中更改 UINavigationBar 标题

swift - 在锁定屏幕上使用 PushKit 和 CallKit 在后台启动应用程序时连接到 WebRTC 后没有声音

ios - 如何通过 voip 和调用套件处理视频调用

push-notification - 我可以在不使用 VoIP 的情况下将 PushKit 用于标准推送通知吗?应用程序在审查中被拒绝的可能性有多大?

ios - 配置文件不包括 com.apple.developer.pushkit.unrestricted-VOIP 权利

ios - 无效 token VoIP Apple 推送通知