【发布时间】:2016-02-22 08:16:52
【问题描述】:
我正在尝试集成 Safari 推送通知。我提到了以下链接。 http://samuli.hakoniemi.net/how-to-implement-safari-push-notifications-on-your-website/#resources
我的客户文件位于... -public_html/push/index.html
服务器配置文件位于... -public_html/push/node_modules/express/index.js
推送包和日志文件位于... -public_html/push/v1/pushPackages/[我的推送 id]/myPackage.zip -public_html/push/v1/pushPackages/[我的推送ID]/log(文件)
我的 index.js 文件有以下代码。
'use strict';
module.exports = require('./lib/express');
//Test code for Safari push notification
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.listen(port);
app.get('/', function(req, res) {
res.sendfile('index.html');
});
app.post('/v1/pushPackages/[my push id]', function(req, res) {
alert('hello');
res.sendfile('myPushPackage.zip');
});
app.post('/v1/log', function(req, res) {
});
我的客户文件 (index.html) 有以下代码:
!doctype html>
<html>
<head>
<title> Safari Push Notifications</title>
</head>
<body>
<h1>Safari Push Notifications</h1>
<a href="#" id="subscribe">Subscribe</a>
<script>
var pushId = [my push id];
document.body.onload = function() {
// Ensure that the user can receive Safari Push Notifications.
if ('safari' in window && 'pushNotification' in window.safari) {
var permissionData = window.safari.pushNotification.permission(pushId);
checkRemotePermission(permissionData);
} else {
alert("Push notifications not supported.");
}
};
var checkRemotePermission = function (permissionData) {
if (permissionData.permission === 'default') {
alert("default.");
// This is a new web service URL and its validity is unknown.
console.log("The user is making a decision");
//alert('The user is making a decision');
var userinfo={};
window.safari.pushNotification.requestPermission(
'https://[mydomain]/push', // The web service URL.
pushId, // The Website Push ID.
userinfo, // Data that you choose to send to your server to help you identify the user.
checkRemotePermission // The callback function.
);
}
else if (permissionData.permission === 'denied') {
alert("denied.");
// The user said no.
console.log('denied');
}
else if (permissionData.permission === 'granted') {
alert("granted.");
// The web service URL is a valid push provider, and the user said yes.
// permissionData.deviceToken is now available to use.
console.log("The user said yes, with token: "+ permissionData.deviceToken);
}
};
</script>
</body>
</html>
它在“拒绝”警报之后给我们“默认”警报。 请帮帮我。
【问题讨论】:
标签: javascript macos safari push-notification