Push notifications on distribution - php

I create all certificates to develope push notify for my app iPhone.
If I test it with file .pem created by developer certificates it work, but if I use file .pem created by production certificates it not work.
This is my code:
public function push($deviceToken,$badge,$message,$deviceType) {
$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;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-prod.pem');
$fp = stream_socket_client("ssl://gateway.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;
fwrite($fp, $msg);
fclose($fp);
}
Any suggest?

Production push certificates will only work when you send notification to an app signed with an ad-hoc distribution certificate (or final release). Developer push certificates will only work with apps signed with a developer distribution certificate (ie installed to the device from XCode).

You can try your production certificate by changing you scheme properties:
Click on your target scheme, on top left of Xcode window, then Edit Scheme.
In the "Run yourTarget" tab, set Release as build configuration and "None" as debugger.
Of course, your target must have an ad-hoc provisionning profile configured.
Plug your iPhone and Run with iOSDevice configuration.
Does it work ?
EDIT : my appologizes, I would say Release instead of Ad-hoc (i corrected it)

Be sure to create a NEW adhoc and or appstore distribution certificate AFTER creating the certificates for the push process. The adhoc and appstore certificates change for push-enables apps.

Related

"Delivered Message to APNS" but not showing notification in Production with ios13 (PHP)

i have a problem with Notifications in ios13. If i use gateway.sandbox i can see the notification, but i'have downloaded the app directly from AppStore. Instead using gateway.apple i see 'Delivered Message to APNS', but nothing on my device.
I also tried to recreate the Certificate, .p12 and .pem file.
Any idea? Thank you
This is my php
public function iOS($data, $devicetoken)
{
// $tHost = 'gateway.sandbox.push.apple.com';
$tHost = 'gateway.push.apple.com';
$tPort = 2195;
$tCert = 'pushcert.pem';
$tPassphrase = 'pushcertpsw12';
$tToken = $devicetoken;
$tSound = 'default';
$tPayload = 'APNS payload';
$tBody['aps'] = array(
'apns-priority' => 10,
'badge' => +1,
'alert' => $data['mtitle'] .' ' .$data['mdesc'],
'sound' => 'default'
);
$tBody ['payload'] = $tPayload;
$tBody = json_encode ($tBody);
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
if (!$tSocket)
exit ('APNS Connection Failed:' .$error. ' ' .$errstr . PHP_EOL);
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
if ($tResult)
echo 'Delivered Message to APNS' . PHP_EOL;
else
echo 'Could not Deliver Message to APNS' . PHP_EOL;
// Close the Connection to the Server.
fclose ($tSocket);
There are two different environments of APNS tokens; Sandbox and Production. They require completely separate setups, permissions, registration, and push setups. Pushing a Sandbox APNS notification to a Production token will not work, the same with Production APNS to Sandbox token.
If you have verified in Sandbox but not in production, you probably need to dupe all your permissions and certificates for production.
Also double check where you're pushing to with your PHP code and ensure it's the correct environment (Sandbox vs Prod).

Push notifications work for development, not production

I have been trying for the past how long to figure out how to go from the sandbox APNS to production APNS. Below is the PHP code used to send notifications to my app.
$passphrase = 'SomethingStrong';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'] . '/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
//return json_encode(array('response' => 'connection_fail'));
}
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
return json_encode(array('response' => 'unsuccessful'));
else
return json_encode(array('response' => 'successful'));
fclose($fp);
This whole thing works when I keep the URL ssl://gateway.sandbox.push.apple.com:2195, but when I when I change it to ssl://gateway.push.apple.com:2195 no notifications come through my app, yet PHP outputs that it was sent successfully.
I'm code-signing with the development certificates.
I'm new to notifications and have never used them before, so sorry if I'm doing something really obvious. Thanks.
There are two things, you need to confirm:
You generate your build with distribution provisioning profile
You use production pem file for sending PUSH from your PHP server
You are setting the correct ssl socket ssl://gateway.push.apple.com:2195 for production.
Check the certificates or the ck.pem file you have upload to server should be production.
Also check for the provisioning profile which you select for build should be Distribution profile.
Everything was setup correctly, but I had an issue with my Ad Hoc profile and the way it was code-signing. All is good now! Thanks!

Push Notification Crypto error

I use Push Notifs in my PHP Laravel app. I created a pem file and tested it. When using it on my dev machine, it correctly pushes to the mobile device. When I now push the whole project to my production server, and start the pushnotif call, I get the error: Failed to enable crypto
Do I need to create a special pem file while being on production server? And I am not talking about "production certificates" I still want to use the sandbox for testing
<?php
namespace App\Helpers;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
class PushNotificationHelper {
public function pushToResponder($deviceToken) {
// Set device token of mobile device and the passphrase for certification
$pushToken = $deviceToken;
$passphrase = Config::get('MFConfig.PushNotificationTest.passPhrase');
$APNS = Config::get('MFConfig.PushNotificationTest.APNS');
// Open new context for streaming and set certificate as well as passphrase
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../App/Certificates/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', '../App/Certificates/entrust_2048_ca.cer');
// Open connection to APNS
$fp = stream_socket_client($APNS, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
// If no connection could be made then fail with error, otherwise connect
if (!$fp) {
exit("Failed to connect: $err, $errstr" . PHP_EOL);
}
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => "MEDIFAKTOR Einsatz",
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $pushToken) . pack('n', strlen($payload)) . $payload;
// Send to server
$result = fwrite($fp, $msg, strlen($msg));
// If no result is available, the message was not delivered.
if(!$result) {
echo 'Message not delivered.' . PHP_EOL;
} else {
echo 'Message successfully delivered.' . PHP_EOL;
}
// Close connection
fclose($fp);
}
}
I tested the connection with:
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert ck.pem -debug -showcerts -CAfile entrust_2048_ca.cer
and it returns status 0 (ok) which is fine I think?
but when I call the function I get: failed loading cafile stream
Not sure about this, but it might be your php settings in the dev enviroment.
try to locate the cacert.pem file.
If you are using a linux system you can try locate cacert.pem using the terminal.
When you find the location find the php.ini file and find this line:
;openssl.cafile =
and change this to
openssl.cafile= path from locate cacert.pem command or the location the .pem is actually located.
Report back on how it went.

Not Getting Push Notification for Production Certificate

I have read many answers on this topic but issue not resolved by suggested solutions. My issue is that I am not getting Push Notifications for Production Certificate. Same Push Notifications are successfully getting in Development certificate. I specially want to say that my device tokens for production and development environment is entirely different. So there is no issue for same device token. Also, i am using different profiles for both. Means to say, there is not configuration issue on app level.
We are using PHP as a server that is sending push notifications.
Here are my two questions:
Is there any thing missing at server side? For which PHP server is sending Push Notifications for development environment successfully and for Production environment, its generating problem?
Am i missing any thing in the app?
I will be very thankful to all of you. I am stuck on this issue. Many Thanks in advance
check php function for iphone push notification...
function iphone_notification($deviceToken,$message,$not_type,$sound,$vibration_type){
$passphrase = '******';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message, 'sound' => $sound);
$body['notification_type'] = $not_type;
$body['vibration_type'] = $vibration_type;
//1 = news;
//99 = other;
//echo $err."<br>".$errstr;
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
}
I faced this same problem. If your .pem file is correct then using following setting you will get push notification. (check terminal commands to make .pem)
//for development profile
$apns_url = 'gateway.sandbox.push.apple.com';
//for production you should use this
$apns_url = 'gateway.push.apple.com';s.
for more detail check this link1 >> link2 >>

Push notification in Production mode is not coming But in development it is coming

My PNS is working good in development mode. I have done this using raywendelich blog. same way i have created certificates in production mode and run same script from server but not receiving any notification.
Which additional step needed when we are testing in production mode from our server.Its very urgent need. plase help what to do for production mode.
our PHP code
<?php
// Put your device token here (without spaces):
$deviceToken = 'd5d89cab86e6f1a3cfa66dd853f3f4d7dd932c4a6da793cb9c86d31e9cfcb31f';
// Put your private key's passphrase here:
$passphrase = '*******';
// Put your alert message here:
$message = '****';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ckm.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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' => $message,
'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);
You need to create the production APNS certificate (very similar to the way you created the developer certificate) and use that to connect to Apple instead. The development and production pushtokens are different so in order to get your production push token, you'll have to build an AdHoc version of your app. When you install the ad-hoc version on your device, it should ask you if you want to recieve push tokens. Accept and your code should send your production push token to your server (I'm assuming you save all pushtokens on a server somewhere). This is the token you need to use to test your production push code.

Categories