Javascript 推送(';注册';)不';我不能在安卓上工作
我正在开发一个应用程序,需要它用Javascript 推送(';注册';)不';我不能在安卓上工作,javascript,cordova,cordova-plugins,Javascript,Cordova,Cordova Plugins,我正在开发一个应用程序,需要它用push.on('registration')保存设备的注册ID,以便将来我可以用它发送通知。它使用的是IOS操作系统,而不是安卓操作系统 var scripts = { initialize: function () { this.bindEvents(); }, bindEvents: function () { document.addEventListener('deviceready', this.onDeviceReady, false
push.on('registration')
保存设备的注册ID
,以便将来我可以用它发送通知。它使用的是IOS操作系统,而不是安卓操作系统
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();
var脚本={
初始化:函数(){
这是bindEvents();
},
bindEvents:函数(){
文件.addEventListener('devicerady',this.ondevicerady,false);
document.addEventListener('deviceregistered',this.onDeviceRegistered,false);
},
onDeviceReady:函数(){
window.open=cordova.InAppBrowser.open;
屏幕。方向。锁定(“纵向”);
var w=Math.max(document.documentElement.clientWidth,window.innerWidth | | 0);
var viewport=document.getElementById(“viewport”);
如果(w<320){
setAttribute(“内容”,“宽度=设备宽度,初始比例=0.68,用户可缩放=否”);
}
navigator.splashscreen.hide();
setTimeout(函数(){
scripts.setupPush();
}, 1000);
},
setupPush:函数(){
var push=PushNotification.init({
“安卓”:{
“senderID”:“125487585225”/*,
“图标”:“推特”,
“iconColor”:“红色”*/
},
“浏览器”:{},
“ios”:{
“senderID”:“125487585225”,
“gcmSandbox”:假,
“声音”:是的,
“振动”:没错,
“徽章”:没错,
“警惕”:正确
},
“窗口”:{}
});
推送('注册'),功能(数据){
警惕(‘teste’);
$.post(webService+“Registro_Dispositivo/Salvar”{
“id”:data.registrationId,
“plataforma”:设备平台
}).always(函数(){
window.localStorage.setItem(appnome+“-registrationId”,data.registrationId);
});
});
push.on('notification',函数(数据){
var href='#/'+data.additionalData.link;
window.location.href=href;
警报(“通知”);
});
push.register(函数(){
警报(“寄存器”);
警报(“正常”);
},函数(){
警报(“Erro”);
});
PushNotification.hasPermission(函数(数据){
警报(data.isEnabled);
警报(“临时许可证”);
});
推送('error',函数(e){
警报(“Erro no Push=“+e.message”);
//console.log(data.registrationId);
});
},
onDeviceRegistered:功能(evt){
//警报(“设备已注册”);
console.log('aqui');
localStorage.setItem(appnome+'-devicePushId',evt.devicePushId);
localStorage.setItem(appnome+'-devicePushToken',evt.devicePushToken);
}
};
scripts.initialize();
如上所述,这段代码在IOS上运行良好,但当我在android上执行它时,什么也没有发生,就像根本没有任何动作一样
提前感谢。确保已在应用程序位置添加“google services.json”文件。 另外,请验证您的Firebase帐户()是否包含应用程序包名称,如“com.yourapp.name”。
验证后,请重新生成应用程序。在连接设备的情况下调试应用程序时,控制台中的输出是什么 无论如何,有很多事情要做,以确保推插件将工作。除了添加google-services.json文件外,您可能还需要编辑config.xml。此外,安装插件的操作在很大程度上取决于您没有提到的cordova CLI和cordova android版本 确保你已经从上到下阅读了插件的页面,你肯定会发现问题出在哪里