ios - didReceiveIncomingPushWith 未调用

标签 ios swift xcode voip pushkit

我正在创建一个 VOIP 应用程序,它使用 pushKit 作为后端来唤醒用户杀死该应用程序或在后台运行的应用程序。

最初,我在 appDelegate 文件中创建了 pushKit 的委托(delegate)

let voipPushResgistry = PKPushRegistry(queue: DispatchQueue.main)

voipPushResgistry.delegate = self

voipPushResgistry.desiredPushTypes = [PKPushType.voIP]

然后我设置它的委托(delegate)

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)
}

之后,我获得了特定应用程序的 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 文件后,它给我消息 消息已成功传递,但遗憾的是,

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {

    print("This is pushKit type ", payload.type)
    completion()
}

上述功能未触发。到底是什么问题导致了这件事的发生呢?如何解决这个问题?

最佳答案

我认为您需要如下图所示的服务。

Check image

如果您已经完成,请在线查看

https://apns-gcm.bryantan.info/

关于ios - didReceiveIncomingPushWith 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569359/

相关文章:

ios - 防止 uitableView 重新加载

ios - 如何使用 Alamofire Swift 4 以 JSON 编码传递数据

ios - 创建一个最喜欢的按钮,快速连接到最喜欢的表格 View

ios - iOS 中另一个请求中的 HTTP 请求

php - 将 '&' 字符串从 xcode 传递到 php?

ios - Swift - 想要在单击 Collection View 单元格内的按钮时进行更改

iphone - 使用 PJSIP 和 PJSUA 的 Peer 2 Peer 调用

ios - iOS 应用程序可以通过 WiFi 与 MFi 配件通信吗?

ios - 如何显示 View 覆盖另一个 View 并更改 View 大小

xcode - Cocoa podspec 和依赖路径