2

デモを使用して、pushNotification を使用してデバイスを登録しようとしました。うまくいきません。「Cordova PushNotification Plugin Demo」と「registing 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>
4

3 に答える 3

6

Hanh Le が言ったように、window-object からアクセスできるようにするには、ecb-callback が必要です。

このような

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

そして交換

     function onNotificationAPN(e) {

    window.onNotificationAPN = function(e) {

EDIT:これはあなたが投稿したコード全体で、私がうまくいくと思う方法で編集されています:

<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>
于 2014-08-21T13:06:57.300 に答える
0

iOSの場合、これに対する解決策を見つけました。この問題の理由はPushPlugin.mcallbackプロパティでnilon kill に設定されているためです。その結果、コールバックで202 行目のチェックが満たされていません- (void)notificationReceived。この問題の修正は、PushPluginアプリが起動するたびにプッシュ通知を開くときにクラスをインスタンス化することです。これは、アプリの起動時にプッシュ通知プラグインを登録することで実行できます。pushNotification.registerアプリが起動するたびに JavaScriptでメソッドを呼び出すと、プラグインがインスタンス化され、適切なcallbackプラグインが割り当てられます。以下のコードをIonicアプリのapp.run()セクションに追加しました

window.plugins.pushNotification.register(successCallback, errorCallback, {
    badge: 'true',
    sound: 'true',
    alert: 'true',
    ecb: 'window.onNotificationAPN'})
于 2016-03-31T07:57:45.210 に答える
0

onNotificationGCM関数をコントローラーの外に置くことができます。それが私にとって最も簡単な解決策です。

  1. 外部jsファイルを作成します例:push.js
  2. またはのようなangularjs APIなしでonNotificationGCM関数を内部に貼り付けますpush.jsangular.module.controller
于 2014-10-01T07:08:07.913 に答える