PushPlugin:找不到变量:onNotificationAPN

时间:2014-03-02 22:23:44

标签: cordova push-notification apple-push-notifications

我正在构建一个使用推送通知的Cordova / Phonegap的iOS应用程序。

我正在使用PushPlugin在客户端应用中实现通知。

我已经设置了APNS方面的内容,当我的应用暂停或关闭时发送通知时,通知会正确显示。但是,当我在应用程序中时,该插件会抛出错误:

Can't find variable: onNotificationAPN

当我查看Xcode输出时,我发现通知已实际发送:

2014-03-02 23:12:58.746 EASP 2014[5792:60b] Notification received
2014-03-02 23:12:58.747 EASP 2014[5792:60b] Msg: {"alert":"testing...",foreground:"1"}

但是在应用程序中没有任何反应,我遇到onNotificationAPN错误。

我已经尝试了一切来调试这个,但我被卡住了。知道为什么会这样吗?

以下是我用来设置通知的代码:

  // SET UP PUSH NOTIFICATIONS
  var pushNotification;
  pushNotification = window.plugins.pushNotification;

  if ( device.platform == 'android' || device.platform == 'Android' ) {
    pushNotification.register(
      successHandler,
      errorHandler, {
        "senderID":"<xxxxx>",
        "ecb":"onNotificationGCM"
      }
    );
  }
  else {
    pushNotification.register(
      tokenHandler,
      errorHandler, {
        "badge":"false",
        "sound":"false",
        "alert":"true",
        "ecb":"onNotificationAPN"
      }
    );
  }

  // result contains any message sent from the plugin call
  function successHandler (result) {
    console.log('result = ' + result);
    navigator.notification.alert(
      result,
      onConfirm,
      'Title of app',
      'Dismiss'
    );
  }

  // result contains any error description text returned from the plugin call
  function errorHandler (error) {
    console.log('error = ' + error);
  }

  function tokenHandler (result) {

    var uuid = device.uuid;
    var platform = device.platform;
    console.log(platform);
    if (platform == 'iOS'){
      var os = 'ios';
    } else {
      var os = 'android';
    }
    hash = result+'<title of app>';
    hash = md5(hash);
    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();

    var url = 'https://<notification server>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os;

    xmlHttp.open( "GET", url, false );

    xmlHttp.send( null );
    console.log(xmlHttp.responseText);
    return xmlHttp.responseText;
  }

  // iOS
  function onNotificationAPN (event) {
    console.log(event);
    if ( event.alert ) {
      navigator.notification.alert(event.alert);
      //alert(event.alert);
    }

    if ( event.sound ) {
        var snd = new Media(event.sound);
        snd.play();
    }

    if ( event.badge ) {
      pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
    }

  }

  function receivedEvent(id) {
    navigator.notification.alert(
      id,
      onConfirm,
      '<title of app>',
      'Dismiss'
    );
  }

  function onConfirm(buttonIndex,id) {
  }

3 个答案:

答案 0 :(得分:3)

经过多次挖掘后管理让它工作。

这是现在的代码:

  // SET UP PUSH NOTIFICATIONS
  var addCallback = function addCallback(key, callback) {
    if (window.pushCallbacks === undefined) {
        window.pushCallbacks = {}
    }
    window.pushCallbacks[key] = callback;
  };


  var pushNotification;
  pushNotification = window.plugins.pushNotification;

  if ( device.platform == 'android' || device.platform == 'Android' ) {
    pushNotification.register(
      successHandler,
      errorHandler, {
        "senderID":"<xxxxx>",
        "ecb":"onNotificationGCM"
      }
    );
  }
  else {
    pushNotification.register(
      tokenHandler,
      errorHandler, {
        "badge":"true",
        "sound":"true",
        "alert":"true",
        "ecb":"pushCallbacks.onNotificationAPN"
      }
    );
  }

  // result contains any message sent from the plugin call
  function successHandler (result) {
    console.log('result = ' + result);
    navigator.notification.alert(
      result,
      onConfirm,
      '<title of app>',
      'Dismiss'
    );
  }

  // result contains any error description text returned from the plugin call
  function errorHandler (error) {
    console.log('error = ' + error);
  }

  function tokenHandler (result) {

    var uuid = device.uuid;
    var platform = device.platform;
    console.log(platform);
    if (platform == 'iOS'){
      var os = 'ios';
    } else {
      var os = 'android';
    }
    hash = result+'<title of app>';
    hash = md5(hash);
    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();

    var url = '<title of app>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os;
    console.log('URL IS: '+url);

    xmlHttp.open( "GET", url, false );

    xmlHttp.send( null );
    console.log(xmlHttp.responseText);
    addCallback('onNotificationAPN', onNotificationAPN);
    return xmlHttp.responseText;

  }

  // iOS
  function onNotificationAPN (event) {
    if ( event.alert ) {
      navigator.notification.alert(event.alert);
    }

    if ( event.sound ) {
        var snd = new Media(event.sound);
        snd.play();
    }

    if ( event.badge ) {
      pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
    }

  }

  function receivedEvent(id) {
    navigator.notification.alert(
      id,
      onConfirm,
      '<title of app>',
      'Dismiss'
    );
  }

  function onConfirm(buttonIndex,id) {
  }

所以基本上添加的是

  var addCallback = function addCallback(key, callback) {
    if (window.pushCallbacks === undefined) {
        window.pushCallbacks = {}
    }
    window.pushCallbacks[key] = callback;
  };

一开始,

"ecb":"pushCallbacks.onNotificationAPN"

注册iOS推送通知时。

立即行动。

答案 1 :(得分:0)

我有同样的问题,最后通过在窗口上显式注册onNotificatianAPN函数来修复它:

var onNotificationAPN = function(event) {
    console.log(event);
    if ( event.alert ) {
      navigator.notification.alert(event.alert);
      //alert(event.alert);
    }

    if ( event.sound ) {
        var snd = new Media(event.sound);
        snd.play();
    }

    if ( event.badge ) {
      pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
    }

  }

if(typeof window.onNotificationAPN === 'undefined'){
    window.onNotificationAPN = onNotificationAPN;
}

答案 2 :(得分:0)

我遇到了同样的问题。基本上,该示例描述了全局范围内的函数。我把我的内容放在一个设置我的事件监听器的函数范围内,因此无法找到它。

(function () {
    document.addEventListener('deviceready', onDeviceReady.bind(this), false);

    function onDeviceReady() {
        // registration happened in here, as a string
        pushNotification.register(,, {...  , "ecb":"onNotificationAPN"});

    }

    function onNotificationAPN(e) {
        // handle notification
    }

})();

我的解决方案是将onNotificationAPN()移到函数外部,使其成为全局函数。它作为字符串传递,而不是作为函数指针/闭包传递。

(function () {
    document.addEventListener('deviceready', onDeviceReady.bind(this), false);

    function onDeviceReady() {
        // registration happened in here
    }

})();

function onNotificationAPN(e) {
    // handle notification
}