firebase通知显示在textview中,而应用程序在前台,但我点击通知消息没有显示在textview android

时间:2017-01-25 09:46:19

标签: 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"));

        }
    };

没有存储消息的其他任何方式吗?

2 个答案:

答案 0 :(得分:0)

在mainActivity的onCreate方法中编写此代码:如果您的通知意图在单击通知时具有数据而不是在textview中打印。

Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
              String msg = bundle.getString("FireBaseNotification");
              fcmTv.setText(msg));
            }

答案 1 :(得分:0)

当触发onMessageReceived()方法时,存储在本地存储(Shared Preference)和onCreate()方法中接收的数据,使用存储在sharedPreference中的值设置文本。如果您不想将其存储在本地存储中,请声明一个静态变量

public static fcm_to_text = "";

使用fcm_to_text变量在onMessageReceived()和onCreate()方法设置textview文本中设置该变量的值。

----------------------------------------------- --------- EDITED ---------------------------------------- ------------------------ 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();
    }