gpt4 book ai didi

android - 我是否需要扩展 FirebaseInstanceIdService 才能订阅 FCM 主题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:32 25 4
gpt4 key购买 nike

我想从客户端(android 应用程序)管理主题订阅。我目前正在 onCreate() Activity 中进行。我想知道正确的方法是在 InstanceIdService::onTokenRefresh() 或任何方便的时间(点击按钮等)订阅/取消订阅。

简而言之,如果我在客户端管理主题订阅(没有服务器),我还需要为 InstanceIdService 操心吗?

不同来源的文档提供了对 Firebase 云消息传递 (FCM) 主题订阅的不同看法。有些人提到了 InstanceIdService,有些人没有。他们在这里:

  1. 关于 Send Topic Messages with Firebase Console 的 Firebase 指南

在谈到主题订阅时,它没有提到 InstanceIdService。

Once you've completed the setup tasks, you can add client code to subscribe to a topic and then handle messages sent to the topic.

Client apps can subscribe to any existing topic, or they can create a new topic. When a client app subscribes to a new topic name (one that does not already exist for your Firebase project), a new topic of that name is created in FCM and any client can subsequently subscribe to it.

To subscribe to a topic, the client app calls Firebase Cloud Messaging subscribeToTopic() with the FCM topic name:

FirebaseMessaging.getInstance().subscribeToTopic("news");
  1. Firebase Android Codelab

The class MyFirebaseInstanceIdService will be a service used to handle FCM logic. This service is used to alert the application when a new InstanceID token is generated, and to retrieve the generated token.

Modify it to extend FirebaseInstanceIdService and override the onTokenRefresh method to subscribe to a topic. Use the following code to update the onTokenRefresh method in MyFirebaseInstanceIdService to look like this:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";
private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage";

/**
* The Application's current Instance ID token is no longer valid
* and thus a new one must be requested.
*/
@Override
public void onTokenRefresh() {
// If you need to handle the generation of a token, initially or
// after a refresh this is where you should do that.
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "FCM Token: " + token);

// Once a token is generated, we subscribe to topic.
FirebaseMessaging.getInstance()
.subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
}
}
  1. Firebase quickstart project on github

它使用 InstanceIdService,但主题订阅不会在那里发生。它作为 Activity 中按钮单击的一部分在客户端中简单地完成:

Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("news");
// [END subscribe_topics]

// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});

InstanceIdService 代码处的注释建议管理器订阅 onTokenRefresh()

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}

最佳答案

如您所知,FirebaseInstanceId 可能是一个单例类,您可以在其中检索注册 token 。所以我认为 subscribeToTopic() 方法,因为只传递了主题的名称,你可以假设它已经调用了 FirebaseInstanceId 本身的实例,或者只是发送在向具有相应注册 token 的 FCM 服务器发出的请求中,以及它应该订阅的主题。

长话短说,我认为您不需要扩展 FirebaseInstanceIdService 来调用 subscribeToTopic(),但是,我认为这是必不可少的,因为(docs 中所述)它是:

Base class to handle Firebase Instance ID token refresh events.


对于您的其他查询。

I want to manage topic subscription from the client (Android app). I am currently doing it at activity onCreate(). I am wondering if the right way is to subscribe / unsubscribe at InstanceIdService::onTokenRefresh() or at any time convenient (on button click, etc)?

我认为在 onCreate() 中执行它是可以的。如果你看到我的回答here ,@FrankvanPuffelen 提到:

subscribing to topics on app start is fine.

但是,我认为在 onTokenRefresh() 中添加一个 subscribeToTopic() 调用也很好,这样一旦为应用程序提供新 token 例如,您将立即相应地订阅它。

注意 我在这里想到的行为是,当注册 token 失效时,它的订阅也会丢失,并且在 onRefreshToken 中添加 subscribeToTopic() () 将立即为您重新订阅它们(当然,这仍然取决于您要订阅哪个主题的实现)。

关于android - 我是否需要扩展 FirebaseInstanceIdService 才能订阅 FCM 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129230/

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