Search code examples
phpapple-push-notificationsapns-php

Hide URL in Payload of push notification


I am sending URL and one message from PHP to ios devices as push notification. what i want is i want to send push notification with one URL and message but URL must be hidden. When user will see PNS on ios device he/she able to see only message but not that URL.like whatsapp messenger

Here is my PHP code

// Open a connection to the APNS server
 $fp = stream_socket_client(
  '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(
       'badge' => +1,
       'alert' =>$message,
       'sound' => 'default'
      );
 // 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));

Solution

  • You should put the URL outside the aps dictionary:

    {
    
        "aps" : {
    
            "alert" : "<Your message>",
    
            "badge" : <your badge>,
    
            "sound" : "default"
    
        },
    
        "url" : "http://www.some.url.com"
    
    }
    

    I don't know php, so I can't give you the exact code.