I´m currently experiencing a problem with the push notification plug-in that i´m using on my app, and i really don´t don´t what am i doing wrong, so i was hopping someone could point me on the right direction. Here is what i am doing:
It´s an app with background geolocation, and i´m using this plug-in. Whenever the user is on a certain radius, a push must happen. The push runs correctly in Foreground and Background(app is open but the user as pressed the main button)...but if the app is closed, the push sends a sound but the notification doesn´t appear and this is not what i´m looking for, and regarding the background geolocation everything is working because i can see the icon on the top of the screen and when the app is downloaded it asks for geolocation even if the user is not on the app. I really think i didn´t configure the push notifications correctly, and this is my code:
Xml file:
<plugin name="phonegap-plugin-push" spec="1.8.2" source="npm">
<param name="SENDER_ID" value="myNumber" />
</plugin>
Js file:( i also started everything from here)
onDeviceReady: function()
{
my code...
app.setupPush();
},
setupPush: function()
{
var push = PushNotification.init({
"android":
{
"senderID": "myNumber"
},
"ios":
{
"sound": true,
"vibration": true,
"badge": true,
"clearBadge": true
},
"windows": {}
});
push.on('registration', function(data)
{
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId)
{
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
deviceToken = data.registrationId;
});
push.on('error', function(e)
{
console.log("push error = " + e.message);
});
push.on('notification', function(data)
{
push.setApplicationIconBadgeNumber(function()
{
console.log('success');
}, function()
{
console.log('error');
}, data.count + 1);
navigator.notification.confirm(
data.message, // message
onConfirm,
'My Title', // title
['Ok', 'X'] // buttonLabels
);
});
}
Php file:
some code to calculate the radius, and then:
if ($ambiente == "Android" && $haParceiros >0 )
{
define( 'API_ACCESS_KEY', 'myKey' );
$registrationIds = array( $_GET['token'] );
// prep the bundle
$msg = array
(
'message' => $pushMessage,
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}
else if ($ambiente == "iOS" && $haParceiros >0 )
{
$device =$deviceToken;
$passphrase = 'myPassphrase';
// Put your alert message here:
$title = $pushMessage;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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(
'alert' => array(
'body' => $title,
'action-loc-key' => 'Eco App',
),
'content-available' => 1,
'image' => 'icon-small.png',
'badge' => $haParceiros,
'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));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
}
and this is it, everything works great in foreground and background, but not when the app is closed, i have read some things hover the web that i don´t know if it can influence, like:
1 - Saving the deviceToken in a database(which i´m not doing, the app registers when it´s open).
2 - the priority of the push, which i think i´m sending on the ios payload with the content available.
I´m sorry for the long issue, thanks for your time..and...Help!!.
Regards