我正在尝试使用PHP将Safari推送通知实现到我的服务器(对于我的网站)。
我正在使用Connor开发的库: https://github.com/connorlacombe/Safari-Push-Notifications
但我不断获得许可:Safari控制台“拒绝”。我正在服务器端打印日志(来自此URL:webServiceURL / version / log),并发现来自Apple的推送包的签名验证失败消息。
在stackOverflow中,我找到了这些:Safari push notifications certificate issue和Apple website push: Signature verification of push package failed, but apple certificates are ok。我已应用他们提供的解决方案,但收到以下错误消息:推送通知包中缺少文件。手动下载pushPackage zip文件我发现应用他们的解决方案时缺少SIGNATURE文件(使用openssl_pkcs7_sign方法中的AppleWWDRCA.pem文件)。
如何摆脱这个问题?怎么做才能在APNS注册 服务?
答案 0 :(得分:1)
在我的项目中实现Safari推送通知之前,我遇到过这种类型的问题。 现在,您正在关注developer.apple.com这个好的,但是this one is also good for reference。
现在已经知道,对于safari中的推送发送,你需要先做三件事
创建您需要的推送包
然后在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');
}
};
这将通过使用您能够发送推送的令牌为您提供设备令牌。
发送推送:
$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);
}
我在这里发送推送给多个用户。 替换所需的文件,然后这将适合您。