2017-12-02 5 views
3

FCMで通知データを解析しようとしています。私はできる限り詳細に問題を説明しようとします。私は2つのアプリにアンドロイドともう1つはjavascript webappが必要です。したがって、webappからpushrootificationをアンドロイドアプリに送信するときに、通知データをjsonstring形式で送信しています。今私は、Java側(アンドロイド)でJSONObjectに変換することができません。以下は私のコードはRemotemessage to JSONオブジェクトのJSONStringであるバンドルを解析します。

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); 
    }) 
}; 

とAndroid側に

@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); 
      } 
     } 
    } 
} 

ですここで私は直接JSONObjectとしてRemotemessageを検討していますが、それは、バンドルjsonstringに来ています。どのように解析するのですか?

出力:それは来ている理由各キーを追加し、実際

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

gcm.notificationも来るべきではない、私は知りません。

enter image description here

+0

エラーは何ですか?リモートメッセージをJSONObjectに変換したいのですか?あなたは実際にこれを必要としません、この場合でもJSONの価値を得ることができますか? –

+0

しかし、私が取得するデータはJSONSTRING形式です。 jsonstringからjsonの値を取得するにはどうすればよいですか? – Stackover67

+0

JSONが必要な場合や、値が必要な場合のみ必要です。 'String userName = data.get(Constants.KEY_USER_DISPLAY_NAME.name())。toString()'これはあなたのために働くかもしれませんか? –

答えて

1

下にJSONの文字列表現からJSONObjectを作って試してみてくださいのようなあなた自身でそれを作成することです。サーバー側のコードでnotificationからdataに変更してください。以下のような -

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); 
    }) 
}; 

とAndroid側であなたのFCMメッセージを取得 -

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

を次に、JavaScriptから割り当てられたキーで値を取得することができます。好きなように - json.get("USER_DISPLAY_NAME");

そして、私はそれがどのように進むのか教えてください。

+0

ヌルオブジェクト参照で仮想メソッド 'java.lang.String com.google.firebase.messaging.RemoteMessage $ Notification.getBody()'を呼び出そうとしています これは私があなたの方法を実装した場合に発生したエラーです。 – Stackover67

+0

'toString()'で 'remoteMessage.getNotification()'に何を与えることができますか? –

+0

あなたと個人的につながることは可能ですか?スカイプやハングアウトのような?私は最後の3日間この問題に苦しんでいた – Stackover67

2

実はあなたはRemoteMessage#のgetData()メソッドからMapオブジェクトを取得しています。 あなたはJSONオブジェクトを必要とするのであれば、何を行うことができますすることは

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

本当にあなたの問題を解決したら、答えを受け入れてください。 – dpaksoni

+0

これを使用すると、私はkeys = 0を得る。 私は画像に自分の出力の写真を追加しています。 – Stackover67

関連する問題