gpt4 book ai didi

java - 如何从 Firebase Push Notification 获取数据并将其显示在您的 Android Activity 中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:52:35 26 4
gpt4 key购买 nike

抱歉这个菜鸟问题,我是 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, 0, 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 Activity 的 FirebaseMessagingService 发送消息。您可以使用 EventBus . Android 优化的事件总线队列简化了 Activity 、 fragment 、线程、服务等之间的通信。更少的代码,更好的质量。

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

关于java - 如何从 Firebase Push Notification 获取数据并将其显示在您的 Android Activity 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com