我是否需要扩展FirebaseInstanceIdService以订阅FCM主题? [英] Do I need to extend FirebaseInstanceIdService to subscribe to FCM Topics?

查看:339
本文介绍了我是否需要扩展FirebaseInstanceIdService以订阅FCM主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

简而言之,如果我在客户端(无服务器)管理主题订阅,是否还需要打扰InstanceIdService?

不同文档来源提供不同的Firebase云消息传递(FCM)主题订阅。有的提到InstanceIdService,有的没有。这里是:


  1. 使用Firebase控制台发送主题消息

通话时没有提及InstanceIdService关于主题订阅。
$ b


完成设置任务后,您可以将客户端代码添加到
订阅主题,然后处理发送给该主题的消息。

客户端应用程序可以订阅任何现有主题,也可以创建一个
的新主题。当客户端应用程序订阅新的主题名称(您的Firebase项目中尚不存在
)时,会在FCM中创建该
名称的新主题,并且任何客户端可以随后订阅
$ it
$ b

要订阅某个主题,客户端应用程序将使用FCM主题名称调用Firebase Cloud Messaging
subscribeToTopic():




  FirebaseMessaging.getInstance()。subscribeToTopic(news); 




  1. Firebase Android Codelab




类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;
$ b $ / **
*应用程序当前的实例ID令牌不再有效
*,因此必须要求新的实例ID令牌。
* /
@Override
public void onTokenRefresh(){
//如果需要处理代码的生成,最初或
//刷新后这是你应该这样做的地方。
String token = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,FCM令牌:+令牌);

//一旦生成一个标记,我们就订阅主题。
FirebaseMessaging.getInstance()
.subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);


code
$ b $ ol $ start $ $
$ li > github上的Firebase快速启动项目

它使用InstanceIdService,但主题订阅不会在那里发生。它是在客户端完成的,作为按钮点击一个活动的一部分:

$ p $按钮subscribeButton = .subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// [START subscribe_topics]
FirebaseMessaging.getInstance ().subscribeToTopic(news);
// [END subscribe_topics]

//记录和吐司
String msg = getString(R.string.msg_subscribed);
Log.d(TAG,msg);
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});

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

$ b $ @ $ $ b $ public void onTokenRefresh(){
//获取更新的InstanceID令牌。
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,Refreshed token:+ refreshedToken);

//如果您想发送消息到这个应用程序实例或
//在服务器端管理这个应用程序订阅,请将
// Instance ID令牌发送到您的应用程序服务器。
sendRegistrationToServer(refreshedToken);




解决方案

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

长话短说,我不认为你需要扩展 FirebaseInstanceIdService 来调用 subscribeToTopic(),但是,我认为这很重要,因为( $ b


处理Firebase实例的基类ID令牌刷新事件。







其他查询


我想要管理topi c从客户端订阅(Android应用程序)。我正在做活动 onCreate()。我想知道是否正确的方法是订阅/取消订阅 InstanceIdService :: onTokenRefresh()或在任何方便的时间(按钮点击等)?


我认为在 onCreate()中可以。如果您在此处看到我的答案,@FrankvanPuffelen提到:


订阅应用程序开始的主题是很好的。


不过,我认为在 subscribeToTopic()调用 onTokenRefresh(),这样一旦为应用程序实例提供了新的令牌,你会马上订阅它。



注意我想这里的行为是,当一个注册令牌失效,它的订阅是也失去了,在 onRefreshToken()中加入 subscribeToTopic()会立即重新订阅它们(当然这仍然取决于你想要订阅的主题的实现。)


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).

In short, if I manage topic subscription at the client side (no server), do I still have to bother with InstanceIdService?

Different sources of documentation provide different take on Firebase Cloud Messaging (FCM) topic subscription. Some mention InstanceIdService, some not. Here they are:

  1. Firebase Guide on Send Topic Messages with Firebase Console

It does not mention InstanceIdService when talking about topic subscriptions.

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

It uses InstanceIdService, but topic subscription does not happen there. It is done simply in client as part of button click in an 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();
    }
});

The comment at InstanceIdService code suggests to manager subscription 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);
}

解决方案

As you already know, FirebaseInstanceId is probably a singleton class, where you retrieve your registration token. So I think the subscribeToTopic() method, since only the name of the topic is passed, you can presume that it already calls an instance of the FirebaseInstanceId itself or then just send in a request towards the FCM servers with the corresponding registration token and the topic it should be subscribed to.

Long story short, I don't think it's a requirement for you to extend the FirebaseInstanceIdService to call subscribeToTopic(), however, I think it's essential because (as described in the docs) it is the:

Base class to handle Firebase Instance ID token refresh events.


For your other inquiries.

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)?

I think doing it in onCreate() is okay. If you see my answer here, @FrankvanPuffelen mentioned:

subscribing to topics on app start is fine.

However, I think it's also good to add in a subscribeToTopic() call in onTokenRefresh(), so that as soon as a new token is provided for the app instance, you'll immediately subscribe it accordingly.

Note that behavior I'm thinking here is that when a registration token is invalidated, it's subscription is also lost and that adding subscribeToTopic() in onRefreshToken() will immediately re-subscribe them for you (of course this still depends in your implementation on which topic you want to subscribe it).

这篇关于我是否需要扩展FirebaseInstanceIdService以订阅FCM主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆