2015-07-15 22 views
7

Eklentiyle ilgili bir sorunum var. Uygulamamdayken ve Parse ile bildirim gönderdiğimde, mesajla bir uyarı alırım (bu amaçlanan şekilde çalışır). Ancak, uygulama arka planda olduğunda, telefonda hiçbir şey görüntülenmez. İşte eklentiyi nasıl kullanırım ve bildirimleri nasıl ele alırım:Ayrıştır CorodvaPush Ionic: Arka planda uygulama olduğunda Android bildirimleri göstermiyor

var gmcId = "xxxxxxx"; 
var androidConfig = { 
    senderID: gmcId 
}; 

document.addEventListener("deviceready", function(){ 
    $cordovaPush.register(androidConfig).then(function(result) { 
    console.log("result: " + result); 
    }, function(err) { 
    // Error 
    }) 

    $rootScope.$on('$cordovaPush:notificationReceived', function(event, e) { 


switch(e.event) 
{ 
    case 'registered': 
    if (e.regid.length > 0) 
    { 

     // 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. 
     console.log("regID = " + 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) 
    { 


     // if the notification contains a soundname, play it. 
     navigator.notification.beep(1); 
     alert(e.payload.data.alert) 
    } 
    else 
    { // otherwise we were launched because the user touched a notification in the notification tray. 
     if (e.coldstart) 
     { 

     } 
     else 
     { 

     navigator.notification.beep(1); 
     alert(e.payload.data.alert) 
     } 
    } 


    break; 

    case 'error': 
    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>'); 
    break; 

    default: 
    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>'); 
    break; 
} 

    }); 

}, false); 

Teşekkürler!

Btw, bu aldığım Ayrıştırma bildirimin yapısı şöyledir:

Yani, saçımı çekerek iki gün sonra nihayet sorunu çözmek için başardı: CEVAP

{"collapse_key":"do_not_collapse", 
"event":"message", 
"foreground":true, 
"from":"xxxxxxxxxx", 
"payload":{ 
"data":  {"alert":"asdasdas","push_hash":"bff149a0b87f5b0e00d9dd364e9ddaa0"},"push_id":"2iFhVp2R4u","time":"2015-07-21T12:24:09.905Z"}} 

. Yani sorun şu ki, JSON'u Parse olarak nasıl belirlediğiniz önemli değildir, her zaman yukarıda sunulan formda gönderir. Bu, belirttiğiniz şeyin her zaman faydalıdır -> veri -> 'anahtar': 'değer'. Ancak, GCMIntentService, JSON'un ilk katmanında "iletiye" sahip bir bildirim ister. Anlamı, öyle görünmek zorunda:

{"collapse_key":"do_not_collapse", 
    "event":"message", 
    "foreground":true, 
    "from":"xxxxxxxxxx", 
    "message":"ssadasdasdsa" 
......} 

Bunu "protected void onMessage" start hatları aşağıda GCMIntentService.java belirtildiğini görebilirsiniz (Ben onun çizgisini 53 düşünüyorum).

Her neyse, bunu düzeltmeniz gerekirse, uygulama ön planda olmadığında bir bildirimin işlenmesini değiştirmemiz gerekir. arasında o DATA- adresinden arayacak> alert (standart form - İşte

@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.d(TAG, "onMessage - context: " + context); 

    // Extract the payload from the message 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     // if we are in the foreground, just surface the payload, else post it to the statusbar 
     if (PushPlugin.isInForeground()) { 
      extras.putBoolean("foreground", true); 
      PushPlugin.sendExtras(extras); 
     } 
     else { 

     try { 
       JSONObject object_example = new JSONObject(extras.getString("data")); 
       String message = object_example.getString("alert"); 
       extras.putString("message", message); 
      } catch (JSONException e) { 
       //some exception handler code. 
      } 

         createNotification(context, extras); 
     } 
    } 
} 

, bizim bildirim JSON ilk katmanında 'mesajı' var doesnt dikkate alacak bizim kod: Biz bunu değiştirmek gerekir PARSE'den bildirim).

Umarım yardımcı olur, çünkü waaay'i günlerce anlamaya çalışıyorum :).

cevap

1

https://github.com/grrrian/phonegap-parse-plugin Yani, saçımı çekerek iki gün sonra nihayet sorunu gidermek için başardı bu Ayrıştırma Eklentisi kullanılmış https://github.com/aaronksaunders/dcww/blob/master/www/js/parseService.js

:

registerCallback: function (_pushCallback) { 
    var deferred = $q.defer(); 
    $window.parsePlugin.registerCallback('onNotification', function() { 

     $window.onNotification = function (pnObj) { 

      _pushCallback && _pushCallback(pnObj); 

      alert('We received this push notification: ' + JSON.stringify(pnObj)); 
      if (pnObj.receivedInForeground === false) { 
       // TODO: route the user to the uri in pnObj 
      } 
     }; 
     deferred.resolve(true); 

    }, function (error) { 
     deferred.reject(error); 
    }); 
    return deferred.promise; 

} 

Burada tam kaynak kodunu görebilirsiniz. Yani sorun şu ki, JSON'u Parse olarak nasıl belirlediğiniz önemli değildir, her zaman yukarıda sunulan formda gönderir. Bu, belirttiğiniz şeyin her zaman faydalıdır -> veri -> 'anahtar': 'değer'. Ancak, GCMIntentService, JSON'un ilk katmanında "iletiye" sahip bir bildirim ister. Anlamı, öyle görünmek zorunda:

{"collapse_key":"do_not_collapse", 
    "event":"message", 
    "foreground":true, 
    "from":"xxxxxxxxxx", 
    "message":"ssadasdasdsa" 
......} 

Bunu "boşluk onMessage korumalı" başlamadan hatları aşağıda GCMIntentService.java belirtildiğini görebilirsiniz (Ben onun çizgisini 53 düşünüyorum).

Her neyse, bunu düzeltmeniz gerekirse, uygulama ön planda olmadığında bir bildirimin işlenmesini değiştirmemiz gerekir.arasında o DATA- adresinden arayacak> alert (standart form - İşte

@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.d(TAG, "onMessage - context: " + context); 

    // Extract the payload from the message 
    Bundle extras = intent.getExtras(); 
    if (extras != null) 
    { 
     // if we are in the foreground, just surface the payload, else post it to the statusbar 
     if (PushPlugin.isInForeground()) { 
      extras.putBoolean("foreground", true); 
      PushPlugin.sendExtras(extras); 
     } 
     else { 

     try { 
       JSONObject object_example = new JSONObject(extras.getString("data")); 
       String message = object_example.getString("alert"); 
       extras.putString("message", message); 
      } catch (JSONException e) { 
       //some exception handler code. 
      } 

         createNotification(context, extras); 
     } 
    } 
} 

, bizim bildirim JSON ilk katmanında 'mesajı' var doesnt dikkate alacak bizim kod: Biz bunu değiştirmek gerekir PARSE'den bildirim).

Umarım yardımcı olur, çünkü waaay'i günlerce anlamaya çalışıyorum :).

0

Özellikle Parse.com push bildirimleri için özel olarak hazırlanmış bir eklenti kullanıyorum. Bildirimler için nasıl kayıt olduğum ve tüm senaryolarda çalışıyorum. -

+0

Parse Eklentisini kullanarak birçok sorun yaşadım, dolayısıyla cordovaPush'a yapıştırmak istedim. Ben belirteci ve userId kayıt ile ilgili herhangi bir sorunum yok, bildirimleri kaydetme ile ilgili bir sorunum var - Ben uyarı (alert.payload.data.alert) ile bir uyarı alabilir, ancak app olduğunda bunu alamıyorum arka fon. – uksz

+0

Tamam, eklenti ile yaşadığınız sorunları merak ettiniz mi? Bugüne kadar 3 müşteri uygulamasında en son sürümün bir çatalını hiç sorun olmadan dağıttık, sağlayabileceğiniz herhangi bir bilgi birikimini takdir edeceksiniz. –

İlgili konular