[英]firebase notification showing in textview while apps in foreground but while i click on notification message not showing in textview android
i want to show fcm notification message in textview and i can show while apps in foreground. 我想在textview中显示fcm通知消息,并且我可以在前台显示应用程序时显示。 but while apps is closed it is not showing in textview. 但是当应用关闭时,它不会显示在textview中。 can anyone please help me. 谁能帮帮我吗。
my codes: 我的代码:
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 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"));
}
};
is ithere any other way without storing message ? 还有其他没有存储消息的方式吗?
write this code in your mainActivity's onCreate method: if your notification intent has data than print in your textview while click on notification. 在mainActivity的onCreate方法中编写以下代码:如果您的通知意图包含的数据比单击通知时在textview中打印的要多。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String msg = bundle.getString("FireBaseNotification");
fcmTv.setText(msg));
}
When onMessageReceived() method is triggered store the data received in local storage ( Shared Preference ) and in onCreate() method set the text using the value stored in sharedPreference. 当onMessageReceived()方法被触发时,将接收到的数据存储在本地存储( Shared Preference )中,在onCreate()方法中,使用sharedPreference中存储的值设置文本。 If you dont want to store it in local storage, declare a static variable 如果您不想将其存储在本地存储中,请声明一个静态变量
public static fcm_to_text = "";
set value of that variable in onMessageReceived() and in onCreate() method set text of textview using fcm_to_text variable. 在onMessageReceived()和onCreate()方法中设置该变量的值,使用fcm_to_text变量设置textview的文本。
--------------------------------------------------------EDITED---------------------------------------------------------------- MyFirebaseMessagingService.java -------------------------------------------------- ------编辑------------------------------------------- --------------------- 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 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.