I am using the following PHP code to send push notifications to the PushWoosh remote API. Its works perfectly for iOS devices and seems to work perfectly for Android (ie I get all the appropriate success responses from the API) but something is amiss with the device registration for Android only as I get InvalidRegistration reports in the PushWoosh console and my Android devices are de-registered from PushWoosh. Anyone shed any light on why? PushWoosh support is virtually none existent (consider this before paying for the service).
The responses messages I am logging are as expected - I get back unknown devices, I register them and receive success response for each registration and I resend the notification and the response message I get back is successful and shows no unknown devices. I then check the push history in the PushWoosh console and the report shows the InvalidRegistration message and devices are removed.
<?php
//creates the notifications setting send time etc - this is not where the problem is
$notifications = $this->create_pushwoosh_notifications($devices);
$data = array("application"=>$appcode,
"auth"=>$apptoken,
"notifications"=>$notifications);
$pw = new push_woosh();
$r = $pw->create_message($data);
if($r){
$response = json_decode($r[1]);
if($response->status_code != 200){
//there was an error
}
error_log($response->status_message);
//retrieve message id's
$messages = $response->response->Messages;
//get any uknown devices associated with the message id's
$unknowns = $response->response->UnknownDevices;
//go through the messages
foreach($response->response->Messages as $message_id){
//act if there is unknowns
if(count($unknowns->$message_id)>0){//if there are unknown devices for a message
//flag that we need to resend once we have finished registering unknown devices
$resend = true;
//change the devices in notifications to the unknowns for the message_id that contained unknown devices
$notifications[$key[0]]['devices']=$unknowns->$message_id;
//loop unknowns
foreach($notifications[$key[0]]['devices'] as $device_id){
$regdata = array(
"application"=>$this->appcode,
"push_token"=>$devices[$device_id]['user_tokenid'],
"language"=>"en", // optional
"hwid"=> $devices[$device_id]['device_id'],//devices indexed by HWID
"timezone"=> 3600, // offset in seconds
"device_type"=>$devices[$device_id]['device_type']
);
}
//register the device
$r = $pw->register($regdata);
if($r){
$response = json_decode($r[1]);
if($response->status_code != 200){
//there was an error
}
error_log($response->status_message);
}else{
//also an error
}
}else{//remove the notification as all devices were known and dont need to resend
unset($notifications[$key[0]]);
}
}
if($resend){//we need to resend
//update $data with new $notifications
$data['notifications']=$notifications;
$r = $pw->create_message($data);
if($r){
$response = json_decode($r[1]);
if($response->status_code != 200){
//there was an error
}
error_log($response->status_message);
}else{
//also an error
}
}
}else{
//there was an error
}