Phonegap - 使用嵌入式网络应用程序推送通知 (InAppBrowser window)
Phonegap - Push notification with embedded web app (InAppBrowser window)
我正在将现有的网络应用程序 (Rails) 包装为网络视图中的 cordova app
。我在 onDeviceReady 上打开主页。随后注册推送通知。
var ref = cordova.InAppBrowser.open(window.app_url,
'_blank', 'location=no,clearsessioncache=no');
setupPushNotifications(window.app_url, ref);
我正在使用 phonegap-plugin-push
推送通知。
push.on('registration', function(data) {
saveRegistrationId(app_url, data);
});
我想在 Web 应用程序中登录用户的上下文中关联通知 registrationId。
这似乎在使用 phonegap 开发者应用程序时顺利进行。但是当我在我的设备上安装 android-debug.apk
时,这不会调用 rails 应用程序来保存 registrationId。
config.xml的内容
<access origin="*" />
<allow-navigation href="http://192.168.1.3:8000" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
我也试过_self
,在同一个网络视图中打开。它不 运行 setupPushNotifications。
注册令牌的代码
function saveRegistrationId(app_url, data) {
// this is our payload for the POST request to server
// data = {registrationId: 'XXXXX', registrationType: 'FCM'}
const device_data = Object.assign(data, this.device);
const url = app_url + "/mobile_devices";
navigator.notification.alert(data.registrationId);
navigator.notification.alert(url);
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url);
httpRequest.onreadystatechange = function() {
navigator.notification.alert('readyState : ' + httpRequest.readyState);
navigator.notification.alert('status : ' + httpRequest.status);
if (httpRequest.readyState>3 && httpRequest.status==200) { console.log(httpRequest.responseText); }
};
httpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(JSON.stringify({device: device_data}));
}
问题出在 index.html 中的 content-security-policy
。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:; connect-src 'self' http://192.168.1.3:8000;">
关键是最后一节
connect-src 'self' http://192.168.1.3:8000;
这允许向网络应用程序发出 ajax 个请求。
我正在将现有的网络应用程序 (Rails) 包装为网络视图中的 cordova app
。我在 onDeviceReady 上打开主页。随后注册推送通知。
var ref = cordova.InAppBrowser.open(window.app_url,
'_blank', 'location=no,clearsessioncache=no');
setupPushNotifications(window.app_url, ref);
我正在使用 phonegap-plugin-push
推送通知。
push.on('registration', function(data) {
saveRegistrationId(app_url, data);
});
我想在 Web 应用程序中登录用户的上下文中关联通知 registrationId。
这似乎在使用 phonegap 开发者应用程序时顺利进行。但是当我在我的设备上安装 android-debug.apk
时,这不会调用 rails 应用程序来保存 registrationId。
config.xml的内容
<access origin="*" />
<allow-navigation href="http://192.168.1.3:8000" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
我也试过_self
,在同一个网络视图中打开。它不 运行 setupPushNotifications。
注册令牌的代码
function saveRegistrationId(app_url, data) {
// this is our payload for the POST request to server
// data = {registrationId: 'XXXXX', registrationType: 'FCM'}
const device_data = Object.assign(data, this.device);
const url = app_url + "/mobile_devices";
navigator.notification.alert(data.registrationId);
navigator.notification.alert(url);
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url);
httpRequest.onreadystatechange = function() {
navigator.notification.alert('readyState : ' + httpRequest.readyState);
navigator.notification.alert('status : ' + httpRequest.status);
if (httpRequest.readyState>3 && httpRequest.status==200) { console.log(httpRequest.responseText); }
};
httpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(JSON.stringify({device: device_data}));
}
问题出在 index.html 中的 content-security-policy
。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:; connect-src 'self' http://192.168.1.3:8000;">
关键是最后一节
connect-src 'self' http://192.168.1.3:8000;
这允许向网络应用程序发出 ajax 个请求。