当前位置 : 主页 > 手机开发 > cordova >

cordova – PushPlugin:找不到变量:onNotificationAPN

来源:互联网 收集:自由互联 发布时间:2021-06-10
我正在构建一个使用推送通知的Cordova / Phonegap的iOS应用程序. 我正在使用PushPlugin在客户端应用程序中实现通知. 我已经设置了APNS方面的东西,当我的应用程序暂停或关闭时发送通知时,通
我正在构建一个使用推送通知的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) {
  }
经过一些挖掘后管理让它工作.

这是现在的代码:

// 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推送通知时.

现在工作.

网友评论