Firebase:这是为 Firebase 控制台创建和订阅主题的正确方法吗?

Firebase: Is this correct way to create and subscribe a topic for firebase console?

我已将我的项目连接到 firebase,我希望在这个应用程序关闭、在后台、前台或被终止时收到通知。 我做了一个按钮,点击它会订阅新闻主题,所以谁能告诉我这是制作主题的正确方法吗?

 public void onClick(View view){

    FirebaseMessaging.getInstance().subscribeToTopic("news");
}

docs 是这样说的:

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted into a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

For example, users of a local weather forecasting app could opt into a "severe weather alerts" topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams.

所以调用 subscribeToTopic 是让用户订阅消息主题的正确方法。您还需要扩展 FirebaseMessagingService 覆盖 onMessageReceived 和 onDeletedMessages。

应用在前台或后台的情况:

  • 当您的应用程序处于后台时发送的通知消息。 在这种情况下,通知会传送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。
  • 具有通知和数据负载的消息,包括背景和 foreground. 在这种情况下,通知被传送到 设备的系统托盘和数据有效负载在附加功能中提供 你的发射器的意图 Activity.