我想从客户端(Android应用程序)管理主题订阅 . 我目前正在onCreate()上进行活动 . 我想知道正确的方法是在InstanceIdService :: onTokenRefresh()上订阅/取消订阅,还是在任何方便(按钮点击等) .
In short, if I manage topic subscription at the client side (no server), do I still have to bother with InstanceIdService?
不同的文档来源对Firebase Cloud Messaging(FCM)主题订阅提供了不同的看法 . 有些人提到InstanceIdService,有些则没有 . 他们来了:
在谈论主题订阅时,它没有提到InstanceIdService .
完成设置任务后,您可以添加客户端代码以订阅主题,然后处理发送到主题的消息 . 客户端应用程序可以订阅任何现有主题,也可以创建新主题 . 当客户端应用程序订阅新主题名称(Firebase项目尚不存在)时,将在FCM中创建该名称的新主题,随后任何客户端都可以订阅该主题 . 要订阅主题,客户端应用程序会使用FCM主题名称调用Firebase Cloud Messaging subscribeToTopic():
FirebaseMessaging.getInstance().subscribeToTopic("news");
类MyFirebaseInstanceIdService将是用于处理FCM逻辑的服务 . 此服务用于在生成新的InstanceID令牌时警告应用程序,并检索生成的令牌 . 修改它以扩展FirebaseInstanceIdService并覆盖onTokenRefresh方法以订阅主题 . 使用以下代码更新MyFirebaseInstanceIdService中的onTokenRefresh方法,如下所示:
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);
}
}
它使用InstanceIdService,但主题订阅不会在那里发生 . 它仅在客户端中作为活动中按钮单击的一部分完成:
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);
}
1 回答
如您所知,
FirebaseInstanceId
可能是一个单例类,您可以在其中检索注册令牌 . 所以我认为subscribeToTopic()
方法,因为只传递了主题的名称,你可以假定它已经调用了FirebaseInstanceId
本身的实例,或者只是向FCM服务器发送一个请求,并带有相应的注册令牌和主题应该订阅 .Long story short ,我要求你延长
FirebaseInstanceIdService
来调用subscribeToTopic()
,但是,我认为这是必要的,因为(如_1457411中所述)它是:为了您的其他询问 .
我认为在
onCreate()
这样做是可以的 . 如果你看到我的回答here,@ FrankkuPuffelen提到:但是,我认为在
onTokenRefresh()
中添加一个subscribeToTopic()
调用也是一件好事,这样一旦为app实例提供了新的令牌,你就会立即相应地订阅它 .Note 该行为我的订阅也会丢失,并且在
onRefreshToken()
中添加subscribeToTopic()
将立即为您重新订阅它们(当然这仍然取决于您要在哪个主题上订阅它的实现) .