Facing problems in ios push notification php code
In production mode don't use sandbox url
$fp = stream_socket_client(
"ssl://gateway.push.apple.com:2195", $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
Production:
ssl://gateway.push.apple.com:2195
.
Development:
ssl://gateway.sandbox.push.apple.com:2195
In my app. I am directly getting deviceToken
that's in NSData
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
// this is producing something like:
// <xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>
//
// and i am directly saving that to my servers database
Here's what i have in my server.
/***
* Function: template_user_info
*
* Parameter
* • $message_input
* - String message
* • $token_array
* - Array contains: `token` ex. "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>"
* Note:
* this removes the '<' and '>' to make the device token valid
* `$device_token = str_replace(">","",str_replace("<","",$device_token));`
* ---
*/
public function __push_notification($message_input, $token_array)
{
$push_config = array(
"development" => array(
"status" => true,
"cert" => realpath('xxx.pem'),
"pass" => 'xxxxxx',
"server" => 'ssl://gateway.sandbox.push.apple.com:2195'
),
"production" => array(
"status" => true,
"cert" => realpath('xxx.pem'),
"pass" => 'xxxxxx',
"server" => 'ssl://gateway.push.apple.com:2195'
)
);
$message = stripslashes($message_input);
foreach ($push_config as $key => $value)
{
if ($value['status'] === true)
$this->__exe_push_notification($message, $value, $token_array);
}
}
private function __exe_push_notification($message, $config, $token_array)
{
$cert = $config['cert'];
$pass = $config['pass'];
$server = $config['server'];
$payload = '{
"aps" :
{
"alert" : "'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client($server, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
// echo "Failed to connect $err $errstr <br>";
return;
}
else
// echo "Post notification sent<br>";
$dev_array = array();
$dev_array = $token_array;
foreach ($dev_array as $device_token)
{
$device_token = str_replace(">","",str_replace("<","",$device_token));
$msg = chr(0) .
pack("n", 32) .
pack('H*', str_replace(' ', '', $device_token)) .
pack("n", strlen($payload)) .
$payload;
// echo "sending message :" . $payload . "n";
fwrite($fp, $msg);
}
fclose($fp);
}
If you still cant send notification check this Troubleshooting Push Notifications
Edit
I've been looking at your push notification push, a while ago, then i've noticed that your 'badge' => "+1"
You cannot increment badge like that.
The only option is to manage it in your app and using database to keep on track..
and maybe it is causing the problem.. and badge
must be a Number, here: Table 3-1
And also check this discussion might help you as well..
Edit 02/12/19 I've updated the php code for push notification to support both development and production.