I had this type of problem before when I implementing Safari push Notification in my project.
Now,You are following the developer.apple.com that good one, but this one is also good for reference.
Now already known that for push send in safari you need three things first
- Create fileName.cer file and CSR file in Mac.
- Create A p12-File with the use of CSR file.
- Building the Push Package
To create push Package You Need
- Create icon.iconset there will be 6 icon these are shown in the push notification.
- Now create website.json file this the most important file in push.
Then Code for Permission for the push in Safari:
window.onload = function () {
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission('web.com.domainname');
checkRemotePermission(permissionData);
}
};
var checkRemotePermission = function (permissionData) {
console.log(permissionData);
if (permissionData.permission === 'default') {
window.safari.pushNotification.requestPermission(
'https://domainname.com',
'web.com.domainname', {},
checkRemotePermission
);
} else if (permissionData.permission === 'denied') {
console.log('denied');
} else if (permissionData.permission === 'granted') {
console.log('granted');
}
};
This will provided you the device Token by using that token you are able to send push.
To Send push:
$title ="title";//Title of the push
$body = "body";//Body of the Push
$button = "View";//view button
$payload['aps']['alert'] = array(
"title" => $title,
"body" => $body,
"action" => $button
);
$payload['aps']['url-args'] = array(
"www.facebook.com" // the sub part of the url to which the subscriber will be redirect after click on the push .This is Add with the URL u given in the website.json file that is:[ "urlFormatString": "http://%@" ] for this url will be :->http://www.facebook.com
);
for($i=0;$i<1;$i++){
$deviceToken =$deviceToken;//This is the DeviceToken that u stored in the DB before.
$payload = json_encode($payload);
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = path/PushCertificates.pem';//Give the path to the ,pem file generated previously from ur registered .p12 file not for the downloaded .p12 file.
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
fclose($apns);
}
Here I am sending the push for multiple users.
Replace the required files and then this will work for you.