如何从推送通知服务中获取消息并显示给我的消息活动

时间:2017-08-27 02:55:10

标签: java android

(这是我的通知)

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, Message.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("FCM Notification");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.drawable.ic_event_note_black_24dp);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
}

和我的留言活动

public class Message extends Activity {

    private TextView  textViewTitle;
    private TextView  textViewContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewContent = (TextView) findViewById(R.id.textViewTitle);

    if (getIntent().getExtras() != null) {
        //init message
        String message = String.valueOf(getIntent().getExtras().get("message"));
        String title = String.valueOf(getIntent().getExtras().get("title"));
        //save the message
        textViewTitle.save(getApplicationContext() , "message" , message);
        textViewContent.save(getApplicationContext() , "title" , title);
        startActivity(new Intent(getApplicationContext() , Message.class));
    }
}

1 个答案:

答案 0 :(得分:0)

MyFirebaseMessagingService内,您发送包含数据的广播。

Intent intent = new Intent("myAction");
intent.putExtra("title", title);
intent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

在您的活动中,您将BroadcastReceiver注册到IntentFilter

public class Message extends Activity {

    private MyReceiver myReceiver;

    @Override
    protected void onStart() {
        super.onStart();
        if (myReceiver == null) {
            myReceiver = new MyReceiver();
        }
        // Take care here. The same action defined in IntentFilter
        // of your MyFirebaseMessagingService must be defined here
        LocalBroadcastManager.getInstance(this)
              .registerReceiver(myReceiver, new IntentFilter("myAction"));
    }

    @Override
    protected void onStop() {
        super.onStop();
        LocalBroadcastManager.getInstance(this)
              .unregisterReceiver(myReceiver);
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Here you get your data
            String title = intent.getStringExtra("title");
            String message = intent.getStringExtra("message");
        }
    }
}