ios - didReceiveIncomingPushWith 未调用
问题描述
我正在创建一个 VOIP 应用程序,它用作pushKit
后端来唤醒用户杀死该应用程序或在后台的应用程序。
pushKit
最初,我在appDelegate
文件中创建了一个代表
let voipPushResgistry = PKPushRegistry(queue: DispatchQueue.main)
voipPushResgistry.delegate = self
voipPushResgistry.desiredPushTypes = [PKPushType.voIP]
然后我设置它的委托
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
print("This is pushKit type ", payload.type)
completion()
}
func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
print("===== This going to invalid token ======", type.rawValue)
}
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
print("======= voip token: \(token)")
UserDefaults.standard.setDeviceToken(value: token)
}
之后,我得到了一个特定应用程序的令牌,我开始使用我在自己的服务器上发布的 PHP 脚本来唤醒该应用程序: 这是 PHP 脚本:
<?php
// Put your device token here (without spaces):
// This is token that gets from appdelegate
$deviceToken = '****************************************';
//
echo $device;
// Put your private key's passphrase here:
$passphrase = '123456';
// Put your alert message here:
$message = 'My first push notification!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'voip.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);
// 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 is successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
在我执行 PHP 文件后,它给了我消息Message is successfully delivered
,但遗憾的是,
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
print("This is pushKit type ", payload.type)
completion()
}
上述功能未触发。导致这件事发生的问题是什么?如何解决这个问题?
解决方案
推荐阅读
- spotify - 如何使用 Spotify api 端点(API 的 URL 包含在下面)播放 index.js 文件中的特定歌曲?
- asciidoc - 如何使用多个说明符来格式化 asciidoc 中的列
- javascript - 如何修复切换动画按钮以覆盖表单输入字段集而不破坏表单
- python - Python中的条件断言
- java - java-library 插件是否完全涵盖了 java 插件功能?
- python - 如何在同一行中获得 2 个 int 和 1 个 float 输入?(Python)
- animation - 从外部屏幕帧动画并转换为内部屏幕
- python - 如何更改 django 中的 url password_reset 链接?
- node.js - 如何将 Zoom 集成到 reactjs 应用程序中?
- javascript - 尝试实施 Ceasar 密码时,我的解密调用返回无效响应,为什么?