[英]firebase notification showing in textview while apps in foreground but while i click on notification message not showing in textview android
我想在textview中顯示fcm通知消息,並且我可以在前台顯示應用程序時顯示。 但是當應用關閉時,它不會顯示在textview中。 誰能幫幫我嗎。
我的代碼:
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// Intent intent=new Intent();
// intent.putExtra("MSG",remoteMessage.getNotification().getBody());
// intent.setAction("imran");
// sendBroadcast(intent);
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String messageBody) {
Intent intent=new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("FireBaseNotification",messageBody);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_email_24dp)
.setContentTitle("Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent.setAction("imran"));
}
廣播接收器:
BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView fcmTv;
fcmTv=(TextView) findViewById(R.id.button);
Toast.makeText(getApplicationContext(),intent.getStringExtra("FireBaseNotification"),Toast.LENGTH_LONG).show();
fcmTv.setText(intent.getStringExtra("FireBaseNotification"));
Log.v("jksdfh",intent.getStringExtra("FireBaseNotification"));
}
};
還有其他沒有存儲消息的方式嗎?
在mainActivity的onCreate方法中編寫以下代碼:如果您的通知意圖包含的數據比單擊通知時在textview中打印的要多。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String msg = bundle.getString("FireBaseNotification");
fcmTv.setText(msg));
}
當onMessageReceived()方法被觸發時,將接收到的數據存儲在本地存儲( Shared Preference )中,在onCreate()方法中,使用sharedPreference中存儲的值設置文本。 如果您不想將其存儲在本地存儲中,請聲明一個靜態變量
public static fcm_to_text = "";
在onMessageReceived()和onCreate()方法中設置該變量的值,使用fcm_to_text變量設置textview的文本。
-------------------------------------------------- ------編輯------------------------------------------- --------------------- MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
SharedPreferences test = getSharedPreferences("MyPrefsFile",0);
SharedPreferences.Editor editor = test.edit();
editor.putString("testing",remoteMessage.getData().toString());
editor.commit();
Log.e("messageData",remoteMessage.getData().toString());
}
}
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SharedPreferences data = getSharedPreferences("MyPrefsFile",0);
Log.e("main",data.getString("testing","novalue"));
}catch (Exception e){
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.