【问题标题】:Cordova PushPlugin register getting ERRORCordova PushPlugin 寄存器出现错误
【发布时间】:2015-12-01 06:06:20
【问题描述】:

我在移动设备中使用 PubNub 推送通知。我为我的项目使用了以下代码。根据以下链接的指示。

https://www.pubnub.com/blog/2014-12-18-sending-android-push-notifications-via-gcm-javascript-using-phonegap/

var pushNotification = window.plugins.pushNotification;

    pushNotification.register(
        successHandler, 
        errorHandler, 
        {
            'senderID':'projectID'
        }
    );

    function successHandler(result) {
        alert('Success: '+ result);
    }
    function errorHandler(error) {
        alert('Error: '+ error);
    }

但是这个推送通知寄存器调用了errorHandler函数。它显示错误消息“找不到类”。

为什么会出现这个错误?

请大家推荐

新更新

根据上面的链接,我在我的 cordova 项目中添加了以下代码。

function initialize() {
    bindEvents();
}
function bindEvents() {
    document.addEventListener('deviceready', init, false);
}

function init() {
    var pushNotification = window.plugins.pushNotification;
    pushNotification.register(successHandler, errorHandler, {'senderID':'projectID','ecb':'onNotificationGCM'});
    // Get your Sender ID at cloud.google.com/console
}

function successHandler(result) {
    alert('Success: '+ result);
}

function errorHandler(error) {
    alert('Error: '+ error);
}

function onNotificationGCM(e) {
    switch( e.event ){
        case 'registered':
            if ( e.regid.length > 0 ){
                console.log('regid = '+e.regid);
                alert(e.regid);
            }
        break;

        case 'message':
            console.log(e);
            if (e.foreground){
                alert('The room temperature is set too high')
            }
        break;

        case 'error':
            alert('Error: '+e.msg);
        break;

        default:
          console.log('An unknown event was received');
          break;
    }
}

initialize();

我在模拟器上运行这段代码。它只是成功调用successHandler 函数"OK"。它没有调用回调函数onNotificationGCM,我也没有得到注册的ID。

我的科尔多瓦项目代码背后的实际问题是什么。

请给我建议

【问题讨论】:

    标签: cordova push-notification cordova-plugins


    【解决方案1】:

    你需要一个ecb事件回调函数,例如:

    pushNotification.register(
      successHandler, 
      errorHandler, 
      {
        'senderID':'your_sender_id',
        'ecb':'onNotificationGCM' // callback function
      }
    );
    

    回调函数应该如下所示:

    function onNotificationGCM(e) {
      case 'registered':
        if (e.regid.length > 0){
          deviceRegistered(e.regid);
        }
      break;
      case 'message':
        if (e.foreground){
          // When the app is running foreground. 
        }
      break;
      case 'error':
        console.log('Error: ' + e.msg);
      break;
      default:
        console.log('An unknown event was received');
      break;
    }
    

    另外,您可以查看插件的 GitHub 存储库中的 readme.md。请注意,该插件已不再维护并标记为“已弃用”。

    【讨论】:

    • 我还添加了 onNotificationGCM 作为回调函数,但我仍然收到同样的错误。
    • 您是否正确包含 PushNotification.js?
    • 我收到了来自 successHandler 的消息。成功结果是好的 > 但它没有调用 onNotificationGCM 函数
    • 嗯……很奇怪。顺便说一句,这似乎是 Cordova 插件本身的问题,与 PubNub API 无关,所以如果您更改主题行,例如“Cordova PushPlugin register getting error ...”,我想更多人会看到它并且帮助你!
    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2018-07-28
    相关资源
    最近更新 更多