java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?

前端之家收集整理的这篇文章主要介绍了java – 如何从Firebase推送通知中获取数据并将其显示在Android Activity中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对不起这个noob问题,我是android开发的新手.我目前正在开发一个项目,需要向安装了我的应用程序的Android设备发送推送通知.我已经按照firebase的快速入门教程完成了这项工作,并在我的设备上成功收到了通知.

问题:如何检索服务器发送的消息并将该消息显示给我的Android Activity?提前致谢.

继承我的代码. MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.tv);

    /*
    * Get the data from push notification and display it in TextView
    * */

    tv.setText("Message retrieve from Push Notification");

}

这是我的MyFirebaseMessagingService.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getNotification().getBody());
}

//This method is only generating push notification
//It is same as we did in earlier posts
public void sendNotification(String messageBody) {
    Intent intent = new Intent(this,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,PendingIntent.FLAG_ONE_SHOT);
    Intent notificationIntent = new Intent(this,MainActivity.class);
    notificationIntent.putExtra("message",messageBody);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    Uri defaultSoundUri =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Peoplelink Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0,notificationBuilder.build());
}
最佳答案
如果您想为您的Android活动发送消息,请使用FirebaseMessagingService.你可以使用EventBus.一个android优化的事件总线que简化了活动,碎片,线程,服务等之间的通信.更少的代码,更好的质量.

将EventBus添加到build.gradle文件

compile 'org.greenrobot:eventbus:3.0.0'

注册MainActivity.java并创建onMessageEvent方法.

public class MainActivity extends AppCompatActivity {

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.tv);

    EventBus.getDefault().register(this);
}

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(String message) {
    textView.setText(message);
};

并在MyFirebaseMessagingService.java中使用EventBus.getDefault().post()

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    EventBus.getDefault().post(remoteMessage.getNotification().getBody());
}

猜你在找的Android相关文章