什么是已弃用的 FirebaseInstanceIdService 和 OnTokenRefresh 的替代方案?

What is an alternative to the deprecated FirebaseInstanceIdService and OnTokenRefresh?

我注意到 FirebaseInstanceIdService 已被弃用,能否请您告诉我我是否正确设置了我的 FirebaseIID 服务和我的 Firebase 消息服务? - 感谢您的宝贵时间!

我现在正在扩展 FireBaseMessagingService 并改为调用 OnNewToken 方法,MyFirebaseIIDService 中的代码现在如下所示:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseIIDService : FirebaseMessagingService
{
const string TAG = "MyFirebaseIIDService";
NotificationHub hub;

public override void OnNewToken(string refreshedToken)
{
    base.OnNewToken(refreshedToken);
    Preferences.Set("notification_token", refreshedToken);
    Log.Debug(TAG, "FCM token: " + refreshedToken);
    SendRegistrationToServer(refreshedToken);
}

void SendRegistrationToServer(string token)
{
    // Register with Notification Hubs
    hub = new NotificationHub(Parameter.NotificationHubName,
                              Parameter.ListenConnectionString, this);

    var tags = GetEnabledCategories();
    var regID = hub.Register(token, tags.ToArray()).RegistrationId;
}

MyFirebaseMessagingService 中的代码如下所示:

 [Service]
 [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
 class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public const string NOTIFICATION_CHANNEL = "us.henrico.gov";
public override void OnMessageReceived(RemoteMessage message)
{
    if (message.GetNotification() != null)
    {
        // Notification payload, not in use, wouldn't trigger this function when in background
        SendNotification(message.GetNotification().Title, message.GetNotification().Body, message.Data["linktype"], message.Data["linkpage"]);
    }
    else
    {
        // Data payload, triggers this function in foreground or background
        SendNotification(message.Data["title"], message.Data["body"], message.Data["linktype"], message.Data["linkpage"]);
    }

}

由于 FirebaseInstanceId 已折旧,您可以使用

  FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
        System.out.println("Token : "+ task.getResult());
    });