2017-12-02 3 views
3

Ich versuche, die Benachrichtigungsdaten in FCM zu analysieren. Ich werde versuchen, mein Problem so detailliert wie möglich zu erklären. Ich muss auf zwei Apps eins ist Android und das andere ist Javascript Webapp. Wenn ich eine Pushnotifikation von webapp an androd app sende, sende ich die Benachrichtigungsdaten im String Format. Jetzt kann ich es nicht in JSONObject auf der Java-Seite (Android) konvertieren. Unten ist mein CodeAnalysiere ein Bündel, das eine JSONString in Rememberessage zu JSON-Objekt ist

var notification = { 
    'TITLE': currentUser.displayName, 
    'MSG': message, 
    'CHAT_KEY': chatKey, 
    'MSG_KEY': 'messageKey', 
    'USER_DISPLAY_NAME': currentUser.displayName, 
    'USER_EMAIL': currentUserEmail, 
    'USER_FCM_DEVICE_ID': toKey, 
    'USER_FCM_DEVICE_ID_SENDER': fromKey, 
    }; 

    fetch('https://fcm.googleapis.com/fcm/send', { 
    'method': 'POST', 
    'headers': { 
     'Authorization': 'key=' + fromKey, 
     'Content-Type': 'application/json' 
    }, 
    'body': JSON.stringify({ 
     'notification': notification, 
     'to': toKey 
    }) 
    }).then(function(response) { 
    console.log(response); 
    }).catch(function(error) { 
    console.error(error); 
    }) 
}; 

Und auf der Android-Seite

@Override public void onMessageReceived(RemoteMessage remoteMessage) { 

    if (remoteMessage.getNotification() != null) { 


     sendDefaultNotification(remoteMessage.getNotification().getTitle(), 
       remoteMessage.getNotification().getBody()); 
    } else { 
     String currentUserEmail = ""; 
     FirebaseAuth auth = FirebaseAuth.getInstance(); 
     if (auth.getCurrentUser() != null && auth.getCurrentUser().getEmail() != null) { 
      currentUserEmail = auth.getCurrentUser().getEmail(); 
     } 

     String userName = remoteMessage.getData().get(Constants.KEY_USER_DISPLAY_NAME); 
     String userEmail = remoteMessage.getData().get(Constants.KEY_USER_EMAIL); 
     String chatKey = remoteMessage.getData().get(Constants.KEY_CHAT_KEY); 
     String deviceId = remoteMessage.getData().get(Constants.KEY_USER_FCM_DEVICE_ID); 
     String deviceIdSender = remoteMessage.getData().get(Constants.KEY_USER_FCM_DEVICE_ID_SENDER); 
     String title = remoteMessage.getData().get(Constants.KEY_MSG_TITLE); 
     String msg = remoteMessage.getData().get(Constants.KEY_MSG); 
     String msgKey = remoteMessage.getData().get(Constants.KEY_MSG_KEY); 

     /*if (chatKey.equals(ConstantsFirebase.FIREBASE_LOCATION_CHAT_GLOBAL)) { 
      title = String.format("%s- %s", title, ConstantsFirebase.CHAT_GLOBAL_HELPER); 
     } else {*/ 
      if (!currentUserEmail.equals(Utils.decodeEmail(userEmail))) { 
       setMessageReceived(FirebaseDatabase.getInstance().getReference() 
         .child(ConstantsFirebase.FIREBASE_LOCATION_CHAT).child(chatKey).child(msgKey) 
         .child(ConstantsFirebase.FIREBASE_PROPERTY_MESSAGE_STATUS)); 
      } 
     /* }*/ 

     boolean notificationIsActive = PreferenceManager.getDefaultSharedPreferences(this) 
       .getBoolean(Constants.KEY_PREF_NOTIFICATION, false); 
     if (auth.getCurrentUser() != null && notificationIsActive) { 
      if (!currentUserEmail.equals(Utils.decodeEmail(userEmail))) { 

       Utils.setAdditionalData(new PushNotificationObject 
         .AdditionalData(title, msg, chatKey, msgKey, userName, 
         userEmail, deviceId, deviceIdSender)); 
       sendNotification(title, msg); 
      } 
     } 
    } 
} 

Hier habe ich die Remotemessage direkt als JSONObject bin am überlegen, aber es ist in Bündel jsonstring kommen. Wie analysiere ich es?

Ausgang:

Bundle[{gcm.notification.USER_DISPLAY_NAME=ishku sukshi, google.sent_time=1512190657773, gcm.notification.TITLE=ishku sukshi, gcm.notification.USER_FCM_DEVICE_ID=fXLDo7zU7c0:APA91bFx0sIGwIZ9jIm7xi7QvSrWKrL29uWJnNT0jujlyVHTScUteuRZ37nB-FgEeBXokZdQfmyGKhhRLjCILraS8sTif4p6DRJ_jZkNlh-J_yhKTAU3WnBYzGBtlaTorcAJhDtd1AIy, gcm.notification.CHAT_KEY=-L-FVx8eZBuz-QIsnXvx, from=1028795933953, [email protected],com, google.message_id=0:1512190657780774%bfd1fc79bfd1fc79, gcm.notification.MSG_KEY=messageKey, gcm.notification.MSG=, gcm.notification.USER_FCM_DEVICE_ID_SENDER=AAAA74kEJQE:APA91bHN5lJf0S8KNXzhU4XL1rz1rqyZ6ziY4UghZudtW6iH84ytQksWMSvSKsaBqQEsw7P2txk-yTGp5DOYElb7pdg8VFgj8wecJUcsPKJ6JCASCO_ihXh6xpo3a2aDuw8HnHPvL0Mr, collapse_key=com.sukshi.sukshichat}] 

Eigentlich gcm.notification jeder Schlüssel auch nicht kommen anhängt, ich weiß nicht, warum das kommt.

enter image description here

+0

Was ist der Fehler, den Sie bekommen? Sie möchten Ihre Remote-Nachricht in JSONObject konvertieren? Sie brauchen das eigentlich nicht, können Sie in diesem Fall auch den Wert von JSON bekommen? –

+0

Aber die Daten, die ich bekomme, sind im JSONSTRING-Format. Wie kann ich Werte von json von jsonstring erhalten? – Stackover67

+0

Sie müssen den JSON haben oder Sie benötigen nur die Werte. 'String userName = data.get (Konstanten.KEY_USER_DISPLAY_NAME.name()). ToString()' könnte das für Sie funktionieren? –

Antwort

1

Versuchen mit JSONObject von String-Darstellung von json machen. Ändern Sie notification zu data auf serverseitigem Code. Wie -

fetch('https://fcm.googleapis.com/fcm/send', { 
    'method': 'POST', 
    'headers': { 
     'Authorization': 'key=' + fromKey, 
     'Content-Type': 'application/json' 
    }, 
    'body': JSON.stringify({ 
     'data': notification, 
     'to': toKey 
    }) 
    }).then(function(response) { 
    console.log(response); 
    }).catch(function(error) { 
    console.error(error); 
    }) 
}; 

und erhalten Sie Ihre fcm Nachricht auf Android Seite -

String body = remoteMessage.getData(); 
JSONObject json = new JSONObject(body); 
Log.d("json", json.toString()); 

Dann können Sie Wert von Schlüssel von Javascript zugewiesen bekommen. Wie - json.get("USER_DISPLAY_NAME");

Und lassen Sie mich wissen, wie es geht.

+0

Versuch, die virtuelle Methode aufzurufen 'java.lang.String com.google.firebase.messaging.RemoteMessage $ Notification.getBody()' für eine Nullobjekt-Referenz Dies ist der Fehler, den ich erhalte, wenn ich Ihren Weg einnehme. – Stackover67

+0

können Sie posten, was 'remoteMessage.getNotification()' mit 'toString()' gibt? –

+0

ist es möglich, mit Ihnen persönlich in Verbindung zu treten? Wie bei Skype oder Hangouts?Ich habe mit diesem Problem seit den letzten 3 Tagen gekämpft – Stackover67

2

Eigentlich Methode Sie bekommen Map-Objekt von RemoteMessage # getData(). Wenn Sie also ein JSON-Objekt benötigen, was Sie tun können, ist es wie auf dem eigenen erstellen unter

JSONObject json = new JSONObject(); 
//data is RemoteMessage#getData(); 
Set<String> keys = data.keySet(); 
for (String key : keys) { 
    try { 
     json.put(key, JSONObject.wrap(data.get(key))); 
    } catch(JSONException e) { 
     //Handle exception here 
    } 
} 
+0

Bitte akzeptieren Sie die Antwort, wenn es Ihr Problem wirklich gelöst hat. – dpaksoni

+0

Mit diesem bekomme ich Schlüssel = 0 Ich füge ein Bild meiner Ausgabe in das Bild. – Stackover67

Verwandte Themen