PhoneGap Push notification APN not play sound - php

i setup Push Notification service "device and Server" for my App , Application received notification in iPhone correctly but with out Sound not play any Advice ??
phone Gap Code
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
try {
initPushNotification();
navigator.notification.vibrate(1000);
} catch (ex) {
alert('error: ' + ex);
}
}
function initPushNotification () {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(
app.successHandler,
app.errorHandler,{
"badge" : "true" ,
"sound" : "ture",
"alert" : "true",
"ecb": "onNotificationAPN"
});
// pushNotification.unregister(app.successHandler, app.errorHandler);
};
server Side Code :
$deviceToken = '**********************3ac58cb70891e04b28';
// Passphrase for the private key (ck.pem file)
// $pass =******* ;
// Get the parameters from http get or from command line
$message = (!empty($_GET['message']))?$_GET['message'] : 'Message text ' ;
$message .= #date("H:i:s d/M/Y", mktime());
$badge = 1;
$sound = 'default';
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'push/pushcert.pem');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
} else {
print "Connection OK";
}
$payload = json_encode($body);
// request one
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
Plugin List :
com.phonegap.plugins.PushPlugin
org.apache.cordova.dialogs
org.apache.cordova.file
org.apache.cordova.media
org.apache.cordova.vibration
thanks for support ...

Related

Apple Push Notifications working in NodeJS but not with PHP on same server

Here is my NodeJS code , which is working totally fine.
var options = {
//production
"cert": readCredentialsFile(cert_file),
"key": readCredentialsFile(cert_key_file),
"production" : production_val,
"passphrase": "mypassphrase",
"gateway": gateway_val,
"port": 2195,
"enhanced": true,
"cacheLength": 5,
"retryLimit":-1
};
options.errorCallback = apnError;
var feedBackOptions = {
"cert": readCredentialsFile(cert_file),
"key": readCredentialsFile(cert_key_file),
"passphrase": "mypassphrase",
"production" : production_val,
"batchFeedback": true,
"interval": 300
};
var apnConnection, feedback;
module.exports = {
initAPN : function(){
try {
apnConnection = new apn.Connection(options);
feedback = new apn.Feedback(feedBackOptions);
feedback.on("feedback", function (devices) {
devices.forEach(function (item) {
//TODO Do something with item.device and item.time;
});
});
} catch (error) {
console.log(error);
}
},
sendIOSNotification : function (device_token,message,params){
var myDevice, note;
try{
console.log("iOS device_token ", device_token);
var randomID = require("random-id");
var payloadMultiCast = {
"data": {
"id": randomID(),
"status": params.notification_type,
"title": message,
"body": params,
"message": message
}
};
myDevice = new apn.Device(device_token);
note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 1;
note.sound = "ping.aiff";
note.alert = message;
note.payload = payloadMultiCast;
console.log("iOS Notification ", note);
if(apnConnection) {
apnConnection.pushNotification(note, myDevice);
console.log("iOS res "+ device_token+":::"+JSON.stringify(apnConnection));
}else{
console.log("NO APNS CONNECTION");
}
}catch(err){
console.log("error:-"+err);
}
}
}
And below is my php code , which is not working with same "pem" file and same credentials:-
$streamContext = stream_context_create();
stream_context_set_option($streamContext, $options);
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60000, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns)
$errrrr = json_encode(error_get_last());
echo("Failed to connect 11amarnew: $err $errstr $errrrr" . PHP_EOL);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $dev)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
fclose($apns);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $file);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$err = ""; $errstr = "";
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
$err,
$errstr,
600000,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$ctx);
if (!$fp)
$errrrr = json_encode(error_get_last());
exit("Failed to connect amarnew: $err $errstr $errrrr" . PHP_EOL);
//echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$message = "Hello Moto!!";
$body['aps'] = array(
'badge' => +1,
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
$deviceToken = "ed3d13a56a92027acbb9805bfa5f9a3d6da4fdcf24a57b375bfb638259e9d31c";
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $dev) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
//echo "<pre>"; print_r($result); die();
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered amar'.$message. PHP_EOL;
// Close the connection to the server
fclose($fp);
The error i am getting is -
Failed to connect amarnew: 0 {"type":2,"message":"stream_socket_client(): unable to connect to ssl:\/\/gateway.sandbox.push.apple.com:2195 (Unknown error)","file":"\/var\/www\/html\/push.php","line":42}
FYI , I have allowed 2195 , 2196 ports also on my sever for PHP , NodeJS doesn't need port to be opened.
Then what is issue that causing not working in PHP and working in NodeJS, finally i have to use the php only hence i need to solve the issue.
Thanks,

Push Notification not receiving on several Devices - only get Push on one Device

i have a little problem with my PHP Code.
I did not get it to work, that the message i sent, will receive on all Devices witch are saved in my Database.
The Push App on the Device registers automatically the deviceToken in my MySQL Database. That is all working fine.
The big problem is, that i get the Push Message only to the FIRST Device from the Database. The other Device don't receive any message.
Here my code:
<?php
require_once('konfiguration.php');
$sql = "SELECT deviceToken FROM DeviceTokens";
$dbquery = mysql_query($sql);
// check checkboxes
$message = $_POST['alert'];
$badge = $_POST['badge'];
$sound = $_POST['sound'];
// Construct the notification payload
$body = array();
// anti-error variable
$nothing = true;
// get user input
if ($message) { $alert=$_POST['message']; $nothing = false;
$body['aps']['alert'] = $alert; }
if ($badge) { $nothing = false;
if($_POST['number']) $number=(int)$_POST['number'];
else $number=1;
$body['aps']['badge'] = $number; }
if ($sound) { $nothing = false;
$body['aps']['sound'] = 'beep.wav'; }
// if input not correct, scream
if ($nothing || ($message && $alert=="")) { echo "Falscher Input!"; exit(); }
//make new stream context
$ctx = stream_context_create();
// set parameters
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'w3workProd.pem');
//stream_context_set_option($streamContext, 'ssl', 'passphrase', 'nils');
$socketClient = stream_socket_client('ssl://gateway.push.apple.com:2195', $error,
$errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload = json_encode($body);
print "sending message :" . $payload . "\n";
while($row = mysql_fetch_array($dbquery)) {
$message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken))
.chr(0) . chr(strlen($payload)) . $payload;
$deviceToken = str_replace(' ', '', $row['deviceToken']);
$message = pack('CnH*', 0, 32, $deviceToken);
$message = $message . pack('n', strlen($payload));
$message = $message . $payload;
fwrite($socketClient, $message);
echo ($row['deviceToken']);
}
fclose($socketClient);
exit();
?>
Would be nice if you can help.
Why is it working with one Device...and not with the others...
have you tried
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT
?

About the apple Enhanced notification format

I use php to send push message to apns , i use "Enhanced notification format" to sent .. but i can not get the return" Codes in error-response packet" anyone can help me ?? here is my code
<?php
header("Content-Type: text/html; charset=UTF-8");
$deviceToken = "123";
$content = "testing";
if(isset($content))
{
$newContent=substr($content,0,30)."...";
$re_content=iconv("GB2312","UTF-8",$newContent);
$pass = 'Ladder';
$body = array("aps" => array("alert" => $re_content, "badge" => 1, "sound" => 'received5.caf'));
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0);
if (!$fp)
{
print "Failed to connect $err $errstrn";
return;
}
else
{
print "Connection OK\n<br/>";
}
$payload = json_encode($body);
$msg =
// new: Command "1"
chr(1)
// new: Identifier "1111"
. chr(1) . chr(1) . chr(1) . chr(1)
// new: Expiry "tomorrow"
. pack('N', time() + 86400)
// old
. chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
//print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
//checkAppleErrorResponse($fp);
echo 'Done\n';
fclose($fp);
echo $apple_error_response = fread($fp, 6);
/* return false;
exit(); */
}
?>
I call this function after fwrite and before close commands
function error_response($fp)
{
$read = array($fp);
$null = null;
$changedStreams = stream_select($read, $null, $null, 0, 1000000);
if ($changedStreams === false)
{
echo ("Error: Unabled to wait for a stream availability");
}
elseif ($changedStreams > 0)
{
$responseBinary = fread($fp, 6);
if ($responseBinary !== false || strlen($responseBinary) == 6)
{
$response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
var_dump($response);
}
}
}
It works when I sending a notification in the enhanced format. But doesn't work with notification in the simple format. I don't have idea why... Any clue?

How to know all devices tokens to send push notification (APNS) in a loop?

I'm trying to use a php code to send to all tokens who downloaded my iPhone application. Could you tell me how to send to multiple devices and how to get into a loop of devices tokens?
this is my code:
<?php
$deviceToken = ''; // HERE I CAN SEND TO ONE DEVICE
// Passphrase for the private key (ck.pem file)
// $pass = '';
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'MY NOTIFICATION BODY';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
// assume the private key passphase was removed.
// stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60,STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
else {
print "Connection OK\n";
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
I wrote a tutorial on push notifications. I suggest you read it so you will better understand what you're supposed to do:
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
Push messages have to be sent one by one. You'll have to use a foreach loop to loop through the device tokens:
Here's a simple example:
foreach ( $device_tokens as $device_token )
{
// Send device token a message here.
}
Where $device_tokens is an array of device tokens.

Push Notifications APNS suddenly stopped working- sandbox - php

I am working on a Iphone app that uses push notification.It was working fine earlier and suddenly stopped working. My php code is
<?php
$deviceToken = ''; // masked for security reason
// Passphrase for the private key (ck.pem file)
$pass = 'appendit';
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'Test Message';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
else {
print "Connection OK \n";
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
I am getting Connection OK sending message :{"aps":{"alert":"Test Message"}} while running from browser. Any idea what may be wrong there ?
Thanks
My guess is that your certificate has expired. I had an expired dev certificate and Apple just accepted the message without complaint, although it never arrived. When I updated the certificate the messages started arriving.

Categories