繁体   English   中英

使用 Xamarin 将带有一些标签的设备再次注册到 azure 通知中心

[英]Register again a device with some tags to a azure notification hub using Xamarin

我正在按照一系列教程创建推送通知 Xamarin 应用程序,特别是 Android,通过Azure 通知中心Firebase接收通知。 这是第一个视频,代码 在 github 中 但是我需要选择应用程序使用标签将设备注册到通知中心的时刻。 目前,它发生在OnNewToken中,但我需要让用户先登录,或者通过输入提供标签,然后我才能注册标签。

解决方案/Project.Android/Model/FirebaseMessagingServiceEx.cs

public override void OnNewToken(string token)
{
    base.OnNewToken(token);

    System.Diagnostics.Debug.WriteLine(token);

    var hub = new NotificationHub(
        Constants.HubName,
        Constants.HubConnectionString,
        MainActivity.Context);

    // register device with Azure Notification Hub using the token from FCM
    var registration = hub.Register(token, Constants.HubTagName);

    // Register template
    var pnsHandle = registration.PNSHandle;
    var templateReg = hub.RegisterTemplate(
        pnsHandle,
        "defaultTemplate",
        Template,
        Constants.HubTagName);

    var receiver = DependencyService.Get<INotificationsReceiver>();
    receiver.RaiseNotificationReceived("Ready and registered...");
}

我什至不明白什么时候调用OnNewToken 我试图强制生成另一个令牌以再次运行OnNewToken ……但未成功。 我可以在检索用户标签后检索当前令牌以执行hub.Register吗? 我看到的所有示例都使用了已弃用的代码。 还有其他选择吗? 谢谢。

不幸的是,Xamarin 的推送通知文档非常不完整,其中很多已弃用/过时。 我努力寻找获取设备令牌并注册 Azure 通知中心的最新机制。

要获取设备令牌,您不应覆盖 OnNewToken。 当前的首选机制如下:

  1. 在您的 Android 项目中,创建一个 C# class 实现Android.Gms.Tasks.IOnCompleteListener
/// <summary>
/// Wraps the native Android interface <see cref="IOnCompleteListener"/>,
/// which enables interaction with Promise-style async callbacks.
/// </summary>
public class GetFcmTokenSuccessHandler : Java.Lang.Object, IOnCompleteListener
{
    private readonly Action<string> _handleFcmTokenCallback;

    public GetFcmTokenSuccessHandler(Action<string> handleFcmTokenCallback)
    {
        _handleFcmTokenCallback = handleFcmTokenCallback;
    }

    /// <summary>
    /// Method invoked when the associated <see cref="Task"/> completes.
    /// </summary>
    /// <param name="result">The task that completed.</param>
    public void OnComplete(Task result)
    {
        if (result.IsComplete && result.IsSuccessful)
        {
            var token = result.Result.ToString();

            _handleFcmTokenCallback?.Invoke(token);
        }
        else if (result.IsComplete && result.Exception != null)
        {
            // Add error handling here
        }
    }

    #region Interface requiresments; irrelevant to implementation
    public JniManagedPeerStates JniManagedPeerState => JniManagedPeerStates.None;

    public void SetJniIdentityHashCode(int value) { }
    public void SetJniManagedPeerState(JniManagedPeerStates value) { }
    public void SetPeerReference(JniObjectReference reference) { }
    #endregion
}
  1. 登录后,在您的 Android 项目的依赖注入的特定平台服务中触发以下代码:
await FirebaseMessaging.Instance
    .GetToken()
    .AddOnCompleteListener(new GetFcmTokenSuccessHandler(token =>
    {
        if (string.IsNullOrWhiteSpace(token))
        {
            // Add error handling here
        }

        // Now create your NotificationHub and register with the token and any tags
    }));

作为附录,如果在应用程序运行时的任何时候您需要再次注册,我建议将设备令牌缓存在 memory 中,这样您就可以再次检索它而无需通过 FirebaseMessaging 实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM