简体   繁体   English

PhoneGap构建推送通知(Android)

[英]PhoneGap Build Push Notification (Android)

I am having trouble receiving any type of callback for the push notifications plugin for phonegap build, I have included the plugin inside config.xml. 我无法接收任何类型的回调用于phonegap构建的推送通知插件 ,我已将该插件包含在config.xml中。

I have signed up to GCM and got my project number needed for pushNotification.register(). 我已经注册了GCM并获得了pushNotification.register()所需的项目编号。

I also have access to the window.plugins.pushNotification object so I know it's included the plugin. 我也可以访问window.plugins.pushNotification对象,所以我知道它包含了插件。

  • PhoneGap Build Version: 3.1 PhoneGap Build版本: 3.1
  • Hydration: Disabled 保湿:禁用
  • Debug: Enabled 调试:启用
  • Device: Samsung Tab 2 设备:三星Tab 2

My index.html js files included are: 我的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>

My config.xml plugins included are: 我的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="*"/>

My app.js call to pushNotification.register() 我的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();

After that is called nothing is executed, app.push_android() is a function of app object. 在调用之后,没有执行任何操作,app.push_android()是app对象的一个​​功能。

If i don't enter a senderID I get an error saying no sender ID so I know that something is working. 如果我没有输入senderID,我会收到错误,说明没有发件人ID,所以我知道某些内容正在运行。 This is so frustrating any ideas? 这是多么令人沮丧的想法?

PS - I also noticed something weird, when I console.log the window.plugins.pushNotification it returns an empty object, however I can still call window.plugins.pushNotification.register(), but I thought I would be visible inside the console.log. PS - 我也注意到一些奇怪的东西,当我调试window.log window.plugins.pushNotification它返回一个空对象,但是我仍然可以调用window.plugins.pushNotification.register(),但我想我会在控制台内看到日志。

I think I've found the solution. 我想我找到了解决方案。

I was passing an integer instead of a string for the property senderID in the object 我在对象中传递属性senderID的整数而不是字符串

Doesnt work 不起作用

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

DOES work 做的工作

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

Try this push notification code - 试试这个推送通知代码 -

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;
    }
}

Refer Link 参考链接

Refer Devgirl's Weblog 参考Devgirl的博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM