【问题标题】:PhoneGap Build Push Notification (Android)PhoneGap 构建推送通知 (Android)
【发布时间】:2014-04-16 13:19:20
【问题描述】:

对于 phonegap 构建的push notifications plugin,我无法接收任何类型的回调,我已将插件包含在 config.xml 中。

我已注册GCM 并获得了 pushNotification.register() 所需的项目编号。

我还可以访问 window.plugins.pushNotification 对象,所以我知道它包含在插件中。

  • PhoneGap 构建版本: 3.1
  • 补水:已禁用
  • 调试:已启用
  • 设备: Samsung Tab 2

我的 index.html js 文件包括:

<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript" src="js/lib/jquery.js" ></script>
<script type="text/javascript" src="js/lib/handlebars.js"></script>
<script type="text/javascript" src="js/handlebars/helpers.js"></script>
<script type="text/javascript" src="js/plugins/fastclick.js"></script>
<script type="text/javascript" src="js/app.js"></script>

我的 config.xml 插件包括:

// plugins
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="com.phonegap.plugins.pushplugin" />
// access to external domains
<access origin="*"/>

我的 app.js 调用 pushNotification.register()

var app = {
init: function() {
    document.addEventListener("deviceready", this.onDeviceReady, false);
    },

    onDeviceReady: function(){
       // DO STUFF
       // ....

       // ENABLE PUSH
       this.push_init();
    },

    push_init: function(){
        app.SENDER_ID = 123456789; // replaced by my actual GCM project no

        var pushNotification = window.plugins.pushNotification;

        pushNotification.register( 
            function(){alert('Push: win');}, // never called
            function(){alert('Push: Error');},  // never called
            { senderID: app.SENDER_ID, ecb: "app.push_android" }
        );
    },
   // never called
   push_android: function(e){
       alert('connection established...');
   console.log( 'successfully started android' );
   console.log( e );
   }

};

// start the app
app.init();

调用后什么都不执行,app.push_android()是app对象的函数。

如果我不输入发件人 ID,我会收到一条错误消息,指出没有发件人 ID,因此我知道某些东西正在工作。有什么想法太令人沮丧了?

PS - 我还注意到一些奇怪的事情,当我 console.log 时 window.plugins.pushNotification 它返回一个空对象,但是我仍然可以调用 window.plugins.pushNotification.register(),但我想我将在 console.log 中可见。

【问题讨论】:

    标签: android cordova push-notification phonegap-plugins


    【解决方案1】:

    我想我已经找到了解决办法。

    我为对象中的属性 senderID 传递了一个整数而不是字符串

    不起作用

    pushNotification.register( 
        function(){alert('Push: win');}, // NOT called
        function(){alert('Push: Error');},  // NOT called
        { senderID: 123456789, ecb: "app.push_android" }
    );
    

    有效

    pushNotification.register( 
        function(){alert('Push: win');}, // called
        function(){alert('Push: Error');},  // called
        { senderID: "123456789", ecb: "app.push_android" }
    );
    

    【讨论】:

      【解决方案2】:

      试试这个推送通知代码 -

      var pushNotification;
      
      document.addEventListener('deviceready', onDeviceReady, true);
      
      function onDeviceReady() {
      
         try {
             pushNotification = window.plugins.pushNotification;
             if (device.platform == 'android' || device.platform == 'Android') {
                 pushNotification.register(successHandler, errorHandler, { "senderID": "123456789", "ecb": "onNotificationGCM" });    // required!
                }
            else {
                pushNotification.register(tokenHandler, errorHandler, { "badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN" }); // required!
                }
            }  
          catch (err) {
              txt = "There was an error on this page.\n\n";
              txt += "Error description: " + err.message + "\n\n";
              alert(txt);
           }
         };
      
      // handle GCM notifications for Android
      function onNotificationGCM(e) {
          switch (e.event) {
              case 'registered':
                  if (e.regid.length > 0) {
                      alert(e.regid);
                      storeToken(e.regid);
                  }
                  break;
      
              case 'message':
                  if (e.foreground) {
                      var my_media = new Media("beep.wav");
                      my_media.play();
                  }
                  else {  
                    // otherwise we were launched because the user touched a notification in the notification tray.
                  }
      
                  break;
      
             case 'error':
                 break;
             default:
                break;
          }
      }
      

      参考Link

      Refer Devgirl's Weblog

      【讨论】:

      • 嗨 Suhas,感谢您的帮助,但这没有用,没有引发错误,所以它让我相信我正在连接到 GCM,但没有调用回调函数。它们肯定存在于我的应用程序中
      • 请参考 Devgirls 博客。如果您在项目中添加了所有java文件并将路径正确地放在“plugins.xml”中,这应该可以工作。并正确插入 pushnotification.js
      • 再次感谢,我最初检查了该网址。就像我在原帖中提到的那样,这不适用于 phonegap CLI,它的 phonegap 构建,我的 www/ 文件夹中根本不会包含任何 java 文件
      • @Suhas 也有解决方案吗? stackoverflow.com/questions/23620218/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多