繁体   English   中英

Firebase通知显示在textview中,而应用程序在前景中,但是当我单击通知消息不显示在textview中时

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM