无法在pubnub javascript回调中获取错误

Not able to get error in pubnub javascript callback

本文关键字:获取 取错误 回调 javascript pubnub      更新时间:2023-09-26

我正在关注这篇博客文章中的操作方法:http://www.pubnub.com/blog/sending-android-push-notifications-via-gcm-javascript-using-phonegap/

到目前为止,我已经做了以下工作:

  • 在Google开发控制台中创建了一个新项目
  • 开启Android版谷歌云消息
  • 已获取发件人ID(项目编号)和服务器钥匙

然后将推送插件添加到现有项目中:

$ cordova plugin add https://github.com/phonegap-build/PushPlugin.git

然后将PushNotification.js从插件中复制到我的js-lib文件夹中。我正在用requirejs加载这个js文件和pubnub-cdn,这似乎很好。

我在howto中用2.2中的脚本创建了一个模块。

define(['env-config','datastore/localstore','jquery','push','pubnub'],function (EnvConfig,Store) {
  var pushNotification = window.plugins.pushNotification;   
  function register() {
    pushNotification.register( 
      successHandler, 
      errorHandler, 
      { 'senderID':EnvConfig.push.senderid, 'ecb':'onNotificationGCM'} 
    );
  } 
  function successHandler(result) { 
    console.log('Success: '+ result); 
  }
  function errorHandler(error) { 
    //** The following line throws: 
    //** java.lang.Long cannot be cast to java.lang.String
    console.log('Error:' + error);
  }
  function onNotificationGCM(e) { 
    switch(e.event){ 
      case 'registered': 
        console.log("Device registered with id "+e.regid);
        Store.set("pushid"+e.regid);
        /*if (e.regid.length > 0) { 
          deviceRegistered(e.regid); 
        } */
        break;   
      case 'message': 
        if (e.foreground){ 
          //What needs to be done when app is in the foreground 
          //while receiving a notification 
          console.log('A notification has been received'); 
        } 
        break;   
      case 'error': 
        console.log('Error: ' + e.msg); 
        break;   
      default: console.log('An unknown event was received'); 
      break; 
    } 
  } 
  var module = {
    init: function() {
      register();
    }
  };
  return module;
});

当设备准备就绪时,我调用这个模块的init函数。

此时register()失败并执行errorHandler回调。但是console.log失败了:java.lang.Long不能转换为java.lang.String。因此我不知道下一步该怎么办。。。

我们非常感谢任何指点。

三星Galaxy S5
Android 4.4.2
Chrome 30.0(在网络视图中)
Cordova 3.6.3-0.2.13

啊,我想我知道发生了什么-您的发件人ID必须是字符串!你可能在用它作为一个数字。

顺便说一句,谢谢你试用我写的教程:-)