【问题标题】:"ecb" doesn't work whit Cordova and PushPlugin“ecb”不适用于 Cordova 和 Push 插件
【发布时间】:2014-08-20 16:47:14
【问题描述】:

我尝试使用 pushNotification 注册我的设备,使用演示。 它不起作用。打印“Cordova PushNotification Plugin Demo”和“registring android”并查看警报“OK”(successHandler 功能有效)。问题是“ecb”不起作用。为什么?你能帮帮我吗?

我用 Cordova 安装的插件有:PushPlugin、Device、Notification、InAppBrowser 和 Network Information。

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

这是代码:

<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>

【问题讨论】:

  • onNotificationGCM 必须在窗口中有范围。查看 logcat 以显示更多错误
  • 那是?没看懂,能举个例子吗?
  • 您运行插件的平台和设备是什么?注意:PushPlugin 在 IOS 模拟器中不工作,您必须在真机上测试。在 Android 中,您可以使用 google API 目标在模拟器中对其进行测试(包括用于 GCM 工作的 google 服务)

标签: cordova phonegap-pushplugin


【解决方案1】:

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

这样

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

然后替换

     function onNotificationAPN(e) {

    window.onNotificationAPN = function(e) {

编辑:这是您发布的全部代码,我认为应该可以编辑:

<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>

【讨论】:

  • 安卓系统呢?我试过这个:window.onNotificationGCM = function(e) { - - - 然后我编辑了这个 window.plugins.pushNotification.register(successHandler, errorHandler, { "senderID": "my_id", "ecb": "window.onNotificationGCM"}); 并再次在这个 plugins.pushNotification.register 中再次使用这个 pushNotification.register,但它们不起作用..
  • 我也尝试过其他组合,但没有。我会等待你的建议。
  • 在 iOS 上,无法在模拟器中测试通知,但在您的 android 设备上它应该可以工作。你是如何发送通知的?如果你的设备注册正确,你能在 logcat 中看到传入的通知吗?
  • 也许我错了。但是使用“onNotificationGCM”功能,他应该打印(使用$("#app-status-ul").append("&lt;li&gt;regID = " + e.regid +"&lt;/li&gt;"))注册ID?现在,我正在使用平板电脑编写代码,但无法更改代码。在 logcat 中,我没有看到任何关于“regID”的信息。但是,对于 Android,正确的方法是什么?我会在 13 号之后做进一步的测试。
  • 在 locgat 中我没有看到任何关于“regID”的信息,因为我没有在日志中“打印”id,而是在“ul”中使用 $("#app-status-ul").append(...
【解决方案2】:

您可以将 onNotificationGCM 函数放在控制器之外。这是最适合我的解决方案。

  1. 创建外部js文件例如:push.js
  2. onNotificationGCM 函数粘贴到push.js 中,而不使用任何angularjs API,例如angular.module.controller

【讨论】:

    【解决方案3】:

    我已经找到了适用于 iOS 的解决方案。此问题的原因是在PushPlugin.m 中,callback 属性在终止应用程序时设置为nil。结果,- (void)notificationReceived 回调中不满足对line#202 的检查。该问题的解决方法是在每次应用启动时在打开推送通知时实例化 PushPlugin 类。这可以通过在应用启动时注册推送通知插件来完成。 每次应用启动时在 javascript 中调用 pushNotification.register 方法,这将实例化插件并分配适当的 callback 插件。我已将以下代码添加到我的Ionic 应用程序的app.run() 部分

    window.plugins.pushNotification.register(successCallback, errorCallback, {
        badge: 'true',
        sound: 'true',
        alert: 'true',
        ecb: 'window.onNotificationAPN'})
    

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 2018-11-04
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多