I am trying to implement Safari Push Notification into my server (For my website) using PHP.
I am following the tutorial below by Apple: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html
I am using the library developed by Connor: https://github.com/connorlacombe/Safari-Push-Notifications
But I am continuously getting permission: "denied" at Safari console. I am printing the log (from this URL: webServiceURL/version/log) at my server end and found that Signature verification of push package failed message from Apple.
In stackOverflow I found these: Safari push notifications certificate issue and Apple website push: Signature verification of push package failed, but apple certificates are ok . I have applied the solution they provided but getting the error message below: Missing file in push notification package . Downloading the pushPackage zip file manually I have found that the SIGNATURE file is missing applying their solution (using the AppleWWDRCA.pem file in openssl_pkcs7_sign method).
How to get rid of this problem? What to do get registered with APNS service?
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
To create push Package You Need
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.