android - 在 Cordova 推送通知

标签 android cordova push-notification phonegap-plugins

我正在使用 cordova 插件添加 phonegap-plugin-push --variable SENDER_ID=XXXXXXXX 这个插件。并将以下代码添加到 javascript 文件中。

   function setupPush() {
   var push = PushNotification.init({
       "android": {
           "senderID": "XXXXXXXX"
       },
       "ios": {
         "sound": true,
         "alert": true,
         "badge": true
       },
       "windows": {}
   });

   push.on('registration', function(data) {
       console.log("registration event: " + data.registrationId);
       var oldRegId = localStorage.getItem('registrationId');
       if (oldRegId !== data.registrationId) {
           // Save new registration ID
           localStorage.setItem('registrationId', data.registrationId);
           // Post registrationId to your app server as the value has changed
       }
   });

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

它给出错误“TypeError:无法调用未定义的方法‘init’”。

最佳答案

如果您在 onDeviceReady 之后还没有这样做,通常会发生这种情况。在此触发之前,您无法访问任何 native 设备 API(推送通知、相机等)。

function onDeviceReady() {

    // Sets up your push notifications 
    setupPush();
}

// Fires when device is ready.
document.addEventListener("deviceready", onDeviceReady, false);

关于android - 在 Cordova 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347547/

相关文章:

SD卡中的Android PhoneGap www文件夹

android - 如何使用 Firebase 推送通知打开 fragment ?

android - Android Studio始终加载支持库的旧版本

android - Quickblox 如何在 android 中记录通话?

jquery - LowLatencyAudio插件:在何处调用指向音频文件的卸载功能?

ios - 如何修复 Xcode 4.3 中为应用程序找到的 "no valid ' aps-environment' 权利字符串?

ios - Ionic 3 + One Signal handleNotificationReceived 不会被触发

android - 在 Android 应用中提交带有 POST 数据的表单

android - 启动 Crashlytics/Fabric Beta 应用程序的 Intent 是什么?

android - 保存在目录中捕获的图像时 PhoneGap 文件 API 问题