【问题标题】:What is an alternative to the deprecated FirebaseInstanceIdService and OnTokenRefresh?已弃用的 FirebaseInstanceIdService 和 OnTokenRefresh 的替代方案是什么?
【发布时间】:2020-04-29 15:02:26
【问题描述】:

我注意到 FirebaseInstanceIdService 已被弃用,请问我是否正确设置了 My FirebaseIID Service 和 My Firebase Messaging Service? - 感谢您的宝贵时间!

我现在扩展 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"]);
    }

}

【问题讨论】:

标签: firebase xamarin xamarin.android firebase-cloud-messaging


【解决方案1】:

由于 FirebaseInstanceId 已贬值,您可以使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2016-08-16
    相关资源
    最近更新 更多