javascript - push.on ('registration' ) 不适用于 android

标签 javascript cordova cordova-plugins

我正在开发一个应用程序,需要它使用 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/

相关文章:

javascript - 在 Angular 6 上为 *ngFor 内的输入类型复选框启用双向数据绑定(bind)

javascript - 使用 JavaScript 的小时和分钟

android - 动画 Gif 图像在 Phonegap 中不起作用

android - 无法通过 Phonegap 中的 Plugman 安装插件

ios - 适用于 ios 的 MQTT 的 Cordova 插件?

cordova - 使用 Capacitor (Ionic) 请求 GPS 许可

javascript - 如果文本或值以字符开头,则运行函数

ios - 架构 libcordova.a 的重复符号

javascript - cordova - "x-ms-webview"访问 dom 元素以执行登录

javascript - OpenShift NodeJS 部署 : socket. io index.html 端口分配等