Phonegap推送通知-PushNotification.init()不触发

时间:2018-09-26 02:27:51

标签: android cordova phonegap

我有一个Phonegap项目,该项目正尝试使用 phonegap-plugin-push 接收推送通知。

我已经注册并设置了Firebase应用(FCM),并下载了 google-services.json 并将其放置在根目录(位于config.xml的旁边)中。

当我在设备上使用Phonegap Mobile Dev App测试时,PushNotification.init()会按预期触发。

** 问题::当我通过Phonegap Build(在线)编译并安装APK时,PushNotification.init()无法启动。

我的config.xml看起来像这样:

<engine name="android" spec="^7.0.0" />
<engine name="browser" spec="^5.0.4" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<plugin name="cordova-plugin-splashscreen" spec="^5.0.2" />
<plugin name="cordova-plugin-network-information" spec="^2.0.1" />
<plugin name="cordova-plugin-file" spec="^6.0.1" />
<plugin name="cordova-plugin-globalization" spec="^1.11.0" />
<plugin name="phonegap-plugin-push" spec="^2.1.3">
    <variable name="FCM_VERSION" value="11.6.2" />
</plugin>
<platform name="android">
    <allow-intent href="market:*" />
    <resource-file src="google-services.json" target="app/google-services.json" />
</platform>

我的index.js看起来像这样:

onDeviceReady: function() {
    console.log('DEVICE READY...');

    var push = PushNotification.init({
        "android": {
            vibrate: true,
            sound: true,
            forceShow: true,
        },
        "browser": {
            pushServiceURL: 'http://push.api.phonegap.com/v1/push'
        },
        "ios": {
            alert: true,
            badge: true,
            sound: true
        }
    });

    push.on('error', function(e) {
        console.log("push error = " + e.message);
    });

    push.subscribe('example', function () {
        console.log('Subscription error:');
        console.log(e);
    });

    PushNotification.hasPermission(function (data) {
        console.log("data.isEnabled = " + data.isEnabled);
    });


    push.on('registration', function (data) {
        console.log(data.registrationId);
        console.log(data.registrationType);
    });

    push.on('notification', function (data) {
        console.log(data.message);
        console.log(data.title);
        console.log(data.count);
        console.log(data.sound);
        console.log(data.image);
        console.log(data.additionalData);
        console.log(navigator.notification);

        navigator.notification.alert(data.message, null, data.title,'Ok');
    });
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在尝试启动和运行推送通知时,我不会遇到很多问题。 并不是您要求的解决方案-到目前为止,cordova-plugin-firebase是我可靠使用的唯一插件。

PS。 如果我没记错的话,他们的iOS安装指南中有一个小错误。

  

点击证书名称左侧的展开箭头以显示   私钥选项

这部分不正确,您需要导出整个证书而不进行扩展。

答案 1 :(得分:0)

最终,我通过完全启动一个新项目并使用github中的代码来实现了基本的推送通知功能(到目前为止,仅在Android上进行了测试)。

在此之后,我在此基础上安装了以前的代码库及其相应的插件,而没有中断任何事情。到目前为止一切顺利。

相关问题