我正在开发一个应用程序,需要它使用 push.on('registration')
保存设备的 register ID
,以便将来我可以发送通知用它。它适用于 IOS,但不适用于 Android。
var scripts = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('deviceregistered', this.onDeviceRegistered, false);
},
onDeviceReady: function () {
window.open = cordova.InAppBrowser.open;
screen.orientation.lock('portrait');
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var viewport = document.getElementById("viewport");
if (w < 320) {
viewport.setAttribute('content', 'width=device-width, initial-scale=0.68, user-scalable=no');
}
navigator.splashscreen.hide();
setTimeout(function () {
scripts.setupPush();
}, 1000);
},
setupPush: function () {
var push = PushNotification.init({
"android": {
"senderID": "125487585225"/*,
"icon": "twitter",
"iconColor": "red" */
},
"browser":{},
"ios": {
"senderID": "125487585225",
"gcmSandbox": false,
"sound": true,
"vibration": true,
"badge": true,
"alert": true
},
"windows": {}
});
push.on('registration', function (data) {
alert('teste');
$.post( webService + 'Registro_Dispositivo/Salvar', {
"id": data.registrationId,
"plataforma": device.platform
}) .always (function (){
window.localStorage.setItem(appnome + "-registrationId", data.registrationId);
});
});
push.on('notification', function (data) {
var href = '#/' + data.additionalData.link;
window.location.href = href;
alert('notification');
});
push.register(function(){
alert('register');
alert("OK");
}, function(){
alert("Erro");
});
PushNotification.hasPermission(function(data){
alert(data.isEnabled);
alert('tem permissão');
});
push.on('error', function (e) {
alert("Erro no Push = " + e.message);
// console.log(data.registrationId);
});
},
onDeviceRegistered: function (evt) {
// alert('Device Registered');
console.log('aqui');
localStorage.setItem(appnome + '-devicePushId', evt.devicePushId);
localStorage.setItem(appnome + '-devicePushToken', evt.devicePushToken);
}
};
scripts.initialize();
如上所述,这段代码在 IOS 上运行良好,但在 android 上执行时没有任何反应,就像根本没有任何 Action 一样。
提前致谢。
最佳答案
确保您已将“google-services.json”文件添加到您的应用程序位置。 另请验证您的 Firebase 帐户 ( https://console.firebase.google.com/ ),其中包含应用程序包名称,例如“com.yourapp.name”。 验证后请重建应用程序。
关于javascript - push.on ('registration' ) 不适用于 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420964/