Visal Sambo
Visal Sambo

Reputation: 1490

didReceiveIncomingPushWith not called

I am creating a VOIP app which used pushKit as backend to wake up app which user killed the app or in background.

Initially, I created a delegate of pushKit in appDelegate file

let voipPushResgistry = PKPushRegistry(queue: DispatchQueue.main)

voipPushResgistry.delegate = self

voipPushResgistry.desiredPushTypes = [PKPushType.voIP]

And then I setup its 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)
}

After, I got a token of a particular app I started to wake up the app by using a PHP script which I posted it on my own server: Here is PHP script:

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

After I executed PHP file and it gives me message Message is successfully delivered but sadly,

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

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

The above function not triggerd. What is the problem that makes this thing happen? How to solve this issue?

Upvotes: 2

Views: 3476

Answers (1)

Bhavesh Nayi
Bhavesh Nayi

Reputation: 715

I think you needed to on service like below image.

Check image

IF you already done then check online

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

Upvotes: 2

Related Questions