2

So I want to target one specific device token via Urban Airship, but no matter what I do, all of my devices get the message intended for a specific device token.

Here's my PHP code - any help is as usual greatly appreciated!

define('APPKEY','XXXXXXXXXXXXXX'); 
define('PUSHSECRET', 'XXXXXXXXXXXXX '); // Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/broadcast/'); 


$msg = "This is a message intended for my iPad 3";

    $devicetokens = array();
$devicetokens[0] = $devicetoken;

 $contents = array(); 
 $contents['badge'] = "1"; 
 $contents['alert'] = $msg; 
 $contents['sound'] = "default"; 

 $push = array("aps" => $contents, "device_tokens" =>$devicetokens); 



 //var_dump($push);


 $json = json_encode($push); 

 $session = curl_init(PUSHURL); 
 curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET); 
 curl_setopt($session, CURLOPT_POST, True); 
 curl_setopt($session, CURLOPT_POSTFIELDS, $json); 
 curl_setopt($session, CURLOPT_HEADER, False); 
 curl_setopt($session, CURLOPT_RETURNTRANSFER, True); 
 curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
 $content = curl_exec($session); 
 //var_dump($content); // just for testing what was sent

 // Check if any error occured 
 $response = curl_getinfo($session);
PinkFloydRocks
  • 808
  • 3
  • 14
  • 29

2 Answers2

5

The URL you're using is for broadcasting. To send notification to specific devices, the url you should use is 'https://go.urbanairship.com/api/push/'

aftab baig
  • 66
  • 1
  • Sorry for the late acceptance! It worked like a charm when I removed the broadcast bit. – PinkFloydRocks Jun 14 '12 at 08:24
  • Just a quick addition, you should remove quotes from badge, for me it was not showing the badge number on the app icon,, but it started showing as soon as i removed quotes. So it should be something like this: $contents['badge'] = 1; – Rohit Gupta Mar 11 '14 at 14:12
1

Urban Airship have provided sample PHP code to overcome your issue.

Check it out here -> https://github.com/urbanairship/php-library/blob/master/sample.php

rosst400
  • 563
  • 2
  • 7
  • 19
  • 1
    It is 404 page. Can toy please help me or guide me how i can send push notification using php to android device – Rizwan Gill Dec 10 '14 at 10:47
  • Maybe they replaced https://github.com/urbanairship/php-library with https://github.com/urbanairship/php-library2? The latter link works. The former does not. – Jaime Montoya Apr 10 '17 at 15:40