2017-08-09 59 views
0

我有一個問題..我怎麼能保存推送通知在textView中我的第二個活動..這是我的代碼。謝謝你的幫助...Firebase在Textview中推送

public class FCM_service extends FirebaseMessagingService { 
public static String dato; 
String type=""; 

TextView texto ; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    if(remoteMessage.getData().size() >0){ 
     type="json"; 
     sendNotificatio(remoteMessage.getData().toString()); 
    } 
    if(remoteMessage.getNotification() !=null) { 
     type = "message"; 
     sendNotificatio(remoteMessage.getNotification().getBody()); 
    } 
} 
public void sendNotificatio(String messageBody) { 
    String id=""; 
    String message=""; 
    String titles=""; 

    if (type.equals("json")){ 
     try { 
      JSONObject jsonObject = new JSONObject(messageBody); 
      id=jsonObject.getString("id"); 
      message=jsonObject.getString("message"); 
      titles= jsonObject.getString("title"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } else if (type.equals("message")) { 
     message= messageBody; 
    } 
    Intent i = new Intent(FCM_service.this,MainActivity.class); 

    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle(titles) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pendingIntent); 
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0,builder.build()); 
    Log.d("LOGTA", "NOTIFICACION RECIBIDA"); 
    Log.d("LOGTAG", "Título:" + titles); 
    Log.d("LOGTAG", "Texto: " + message); 
    String dato = message; 
    i.putExtra("MENSAJE", dato); 
    Log.e("Mensajito", dato); 

} 
} 

我試圖用意圖/捆把消息發送給其他活動......但我總是收到空,異常錯誤..別人的幫助,請

+0

想要保存什麼,數據,消息? –

+0

是布魯諾。我收到服務器消息...,但我想要做的是,在textView中捕獲此服務器消息到我的第二個活動 –

回答

1

剛剛嘗試這一點:

Intent i = new Intent(FCM_service.this,MainActivity.class); 
i.putExtra("id",id); 
i.putExtra("message",message); 
i.putExtra("titles",titles); 

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT); 

而在其他活動:

String id=getIntent().getStringExtra("id"); 
String message=getIntent().getStringExtra("message"); 
String titles=getIntent().getStringExtra("titles"); 

設置文本

textview.setText(message); 
+0

謝謝你一個BRUNO你保存我的日子#RESPECT –

+0

沒問題只是接受答案,如果你想並投票:) @WidMaer –

+0

投票?我怎樣才能做到這一點 ? –