0

私のアプリケーションにアンドロイド通知を実装しましたが、実際のメッセージ本文の代わりに番号が表示されている点を除いて正常に動作しています。ここで間違って行くいただきました!ここで私は何を取得しています、アンドロイド通知の代わりに番号を取得

enter image description here

のtehのスクリーンショットがあるこれは私が持っているコード、

public static final int MESSAGE_NOTIFICATION_ID = 435345; 
    private int MESSAGE_TYPE ; 


    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     String type = data.getString("type"); 
     if(type.equalsIgnoreCase("Load Messages")) 
     { 
      MESSAGE_TYPE = Global.NOTIFICATION_LOAD_MESSAGE; 
      EventBus.getDefault().post(new HandyManEvents.ReloadMessages(true)); 
     } 
     else 
     { 
      MESSAGE_TYPE = Global.NOTIFICATION_LOAD_LIVE_JOBS; 
     } 
     createNotification(from, message); 
    } 

    // Creates notification based on title and body received 
    private void createNotification(String title, String body) { 

     Context context = getBaseContext(); 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     notificationIntent.putExtra("menuFragment", MESSAGE_TYPE); 
     PendingIntent pending= PendingIntent.getActivity(context, 0,notificationIntent, 0); 


     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title) 
       .setContentIntent(pending) 
       .setContentText(body); 
     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); 

    } 

任意の手がかりとなりますか?

アプリがフォアグラウンドで実行されているときに、この現象が発生しています。これは通知から得たバンドルです

Bundle[{type=Load Messages, notification=Bundle[{e=1, body=You have a new message, icon=app_icon, title=New Message}], collapse_key=com.company.app}] 

バンドルからタイトルと本文を抽出するにはどうすればよいですか?

ありがとうございました。

+0

'data.getString(" message ")はおそらくその値です。 「バンドルデータ」はどこから来ますか? – CommonsWare

+0

バンドルデータがサーバーから送信されています。これは主に、アプリを開いたまま通知を受け取った場合に発生します。 – Zach

答えて

0

Google PlayサービスAPIを使用してGCMメッセージをキャプチャしています。このコードが属するクラスは、GcmListenerServiceを拡張するもので、最初の引数(from)として送信者IDを取り、タイトルとして割り当てる際に通知に表示される内容をオーバーライドします(onMessageReceived(String from, Bundle data))。

データを取得できる正しい方法でバンドルを解析する必要があります。これは、サーバーが送信するペイロードに依存します。どのキーがバンドル内で利用可能であるかは、ログで確認できます。

for (String key : bundle.keySet()){ 
    Log.d(TAG, key + " = " + bundle.get(key)); 
} 
+0

お返事ありがとうございます。あなたは正しいですが、私はフォアグラウンドでアプリを持っている場合にのみ起こります。その背景に私は適切な通知を得ている場合。更新された質問をご覧ください。 – Zach

+0

質問を修正して別の質問を提示しないでください。 –

関連する問題