PhoneGap Build 推送通知(Android)

9

我遇到了问题,无法接收任何形式的回调来使用PhoneGap Build的推送通知插件,我在config.xml文件中包含了该插件。

我已经注册了GCM并获取到了用于pushNotification.register()的项目编号。

我也可以访问window.plugins.pushNotification对象,所以我知道它已经包含了该插件。

  • PhoneGap Build版本:3.1
  • 水合:禁用
  • 调试:启用
  • 设备:三星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对象的一个函数。

如果我不输入senderID,就会收到一个错误,指出没有发送者ID,所以我知道某些东西正在起作用。这真让人沮丧,你有什么想法吗?

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

2个回答

4

我想我已经找到解决方案了。

在对象的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;
    }
}

参见链接

请参阅 Devgirl 的博客


嗨,Suhas,感谢您的帮助,但这没起作用,也没有抛出任何错误,所以我认为我正在连接到GCM,但回调函数没有被调用。它们绝对存在于我的应用程序中。 - pleshy
请参考Devgirls博客。如果您已将所有Java文件添加到项目中并在“plugins.xml”中正确设置了路径,并正确插入了pushnotification.js,则应该可以正常工作。 - Suhas Gosavi
再次感谢,我已经检查了那个原始链接。就像我在原帖中提到的,这不是针对phonegap CLI,而是针对phonegap build,我的www/文件夹中不会包含任何Java文件。 - pleshy
@Suhas,这个问题有解决方案吗?http://stackoverflow.com/questions/23620218/phonegap-push-plugin-not-working-on-android-4-0-4-and-below - mirza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接