“ecb”不工作惠普Cordova和PushPlugin [英] "ecb" doesn't work whit Cordova and PushPlugin

查看:111
本文介绍了“ecb”不工作惠普Cordova和PushPlugin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用pushNotification注册我的设备,使用演示。
它不工作。打印Cordova PushNotification插件演示和注册Android,看看警报OK(successHandler函数工作)。问题是,不工作的ecb。为什么?你能帮助我吗?

I've tried to register my device using pushNotification, using a demo. It doesn't work. Print "Cordova PushNotification Plugin Demo" and "registering android" and look the alert "OK" (successHandler function works). The problem is that doesn't work the "ecb". Why? Can you help me?

我用Cordova安装的插件有:PushPlugin,Device,Notification,InAppBrowser和网络信息。

The plugin that I've installed with Cordova are: PushPlugin, Device, Notification, InAppBrowser and Network Information.

我试过在我的LG L9和模拟器上的应用程序。同样的问题。

I've tried the app on my LG L9 and on emulator. Same problem.

这是代码:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>        
<script type="text/javascript">
var pushNotification;

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available

function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
    $("#app-status-ul").append("<li>registering android</li>");
    window.plugins.pushNotification.register(successHandler, errorHandler, {
        "senderID": "my_id",
        "ecb": "onNotificationGCM"
    }); // required!
} else {
    $("#app-status-ul").append("<li>registering iOS</li>");
    pushNotification.register(tokenHandler, errorHandler, {
        "badge": "true",
        "sound": "true",
        "alert": "true",
        "ecb": "onNotificationAPN"
    }); // required!
}
}

// handle APNS notifications for iOS

function onNotificationAPN(e) {
if (e.alert) {
    navigator.notification.alert(e.alert);
}
if (e.sound) {
    var snd = new Media(e.sound);
    snd.play();
}
if (e.badge) {
    pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android

function onNotificationGCM(e) {
navigator.notification.alert(e.event);
switch (e.event) {
case 'registered':
    if (e.regid.length > 0) {
        navigator.notification.alert(e.regid);
        // Your GCM push server needs to know the regID before it can push to this device
        // here is where you might want to send it the regID for later use.
        $("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
        sessionStorage.setItem("deviceId",e.regid);
    }
    break;
case 'message':
    // if this flag is set, this notification happened while we were in the foreground.
    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
    if (e.foreground) {
        navigator.notification.alert('--INLINE NOTIFICATION--');
        // if the notification contains a soundname, play it.
        var my_media = new Media("/android_asset/www/" + e.soundname);
        my_media.play();
    } else { // otherwise we were launched because the user touched a notification in the notification tray.
        if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
        else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
    }
    navigator.notification.alert(e.payload.message);
    navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
    break;
case 'error':
    navigator.notification.alert('ERROR -> MSG:' + e.msg);
    break;
default:
    navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
    break;
}
}

function tokenHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "APNS");
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}

function successHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "GCM");
}

function errorHandler(error) {
navigator.notification.alert(error, null, 'Alert', 'OK');
}

        document.addEventListener('deviceready', onDeviceReady, true);

     </script>
    <div id="home">
        <div id="app-status-div">
            <ul id="app-status-ul">
                <li>Cordova PushNotification Plugin Demo</li>
            </ul>
        </div>
    </div>


推荐答案

正如Hanh Le所说,你需要有ecb -callback可以从窗口对象访问

As Hanh Le said, you need to have the ecb-callback to be accessible from the window-object

像这样

    pushNotification.register(tokenHandler, errorHandler, {
       "badge": "true",
       "sound": "true",
       "alert": "true",
       "ecb": "window.onNotificationAPN"
    }); // required!

,然后替换

     function onNotificationAPN(e) {

    window.onNotificationAPN = function(e) {


$ b b

编辑:这里是我认为应该工作的方式编辑的整个代码:

here is the whole code you posted edited in a way I think should work:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>        
<script type="text/javascript">
var pushNotification;

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available

function onDeviceReady() {
    pushNotification = window.plugins.pushNotification;
    if (device.platform == 'android' || device.platform == 'Android') {
        $("#app-status-ul").append("<li>registering android</li>");
        pushNotification.register(successHandler, errorHandler, {
           "senderID": "my_id",
           "ecb": "window.onNotificationGCM"
        }); // required!
    } else {
         $("#app-status-ul").append("<li>registering iOS</li>");
         pushNotification.register(tokenHandler, errorHandler, {
            "badge": "true",
            "sound": "true",
            "alert": "true",
            "ecb": "window.onNotificationAPN"
         }); // required!
   }
}

// handle APNS notifications for iOS

window.onNotificationAPN = function(e) {
   if (e.alert) {
      navigator.notification.alert(e.alert);
   }
   if (e.sound) {
      var snd = new Media(e.sound);
      snd.play();
   }
   if (e.badge) {
      pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
   }
}
// handle GCM notifications for Android

window.onNotificationGCM = function(e) {
   navigator.notification.alert(e.event);
   switch (e.event) {
      case 'registered':
      if (e.regid.length > 0) {
          navigator.notification.alert(e.regid);
          // Your GCM push server needs to know the regID before it can push to this    device
         // here is where you might want to send it the regID for later use.
         $("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
         sessionStorage.setItem("deviceId",e.regid);
     }
     break;
     case 'message':
        // if this flag is set, this notification happened while we were in the foreground.
        // you might want to play a sound to get the user's attention, throw up a dialog, etc.
         if (e.foreground) {
            navigator.notification.alert('--INLINE NOTIFICATION--');
            // if the notification contains a soundname, play it.
            var my_media = new Media("/android_asset/www/" + e.soundname);
            my_media.play();
         } else { // otherwise we were launched because the user touched a notification in the notification tray.
           if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
           else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
         }
         navigator.notification.alert(e.payload.message);
         navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
         break;
    case 'error':
       navigator.notification.alert('ERROR -> MSG:' + e.msg);
    break;
    default:
       navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
    break;
  }
}

function tokenHandler(result) {
   navigator.notification.alert(result, null, 'Alert', 'OK');
   sessionStorage.setItem("deviceId", result);
   sessionStorage.setItem("notificationServer", "APNS");
   // Your iOS push server needs to know the token before it can push to this device
   // here is where you might want to send it the token for later use.
}

function successHandler(result) {
   navigator.notification.alert(result, null, 'Alert', 'OK');
   sessionStorage.setItem("deviceId", result);
   sessionStorage.setItem("notificationServer", "GCM");
}

function errorHandler(error) {
   navigator.notification.alert(error, null, 'Alert', 'OK');
}

document.addEventListener('deviceready', onDeviceReady, true);

</script>
<div id="home">
    <div id="app-status-div">
        <ul id="app-status-ul">
            <li>Cordova PushNotification Plugin Demo</li>
        </ul>
    </div>
</div>

这篇关于“ecb”不工作惠普Cordova和PushPlugin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆