Firebase推送Textview

时间:2017-08-09 14:18:14

标签: android textview

我有一个问题..如何将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);

}
}

我试图使用intent / bundle将消息发送到其他活动...但我总是收到null和异常错误..有人帮忙请

1 个答案:

答案 0 :(得分: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);