【问题标题】:iOS Push Notifications with Azure Notification Hub使用 Azure 通知中心的 iOS 推送通知
【发布时间】:2021-08-03 07:25:26
【问题描述】:

我在 Xamarin Forms 项目中让推送通知在 iOS 中工作完全没有运气。

在 AppDelegate.cs 中,我在 FinishedLaunching 覆盖中调用以下内容:

MSNotificationHub.Start("Endpoint=sb://[redacted].servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=[redacted]",
                        "[redacted]");

用户在应用程序生命周期中进一步登录后,我还使用他们的用户标签注册用户,如下所示:

    public async Task UpdateTags(string token)
    {
        await Task.Run(() =>
        {
            try
            {
                // No point registering tags until the user has signed in and we have a device token
                if (CurrentAccount == null)
                {
                    Console.WriteLine($"UpdateTags cancelled: Account is null");
                    return;
                }
                var tag = $"user:{CurrentAccount.UserName}";
                Console.WriteLine($"Registering tag: {tag}");
                MSNotificationHub.AddTag(tag);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error registering tag: {e.ToString()}");
            }
        });
    }

我已在通知中心正确配置 Apple (APNS) 设置,使用令牌身份验证模式(多次验证四个字段)。证书(签名身份)是“iOS 分发”,标识符包与我在配置中的完全匹配(不使用通配符),密钥启用了 Apple 推送通知服务(APN),配置文件具有平台:iOS 和类型:应用商店。

我将应用程序推送到 TestFlight,因为我无法访问物理 Mac(我们使用 Cloud mac 进行开发)。当我从安装了应用程序的个人 iPhone 查看设备日志时,我在运行它时看到以下内容:

<Notice>: Registered for push notifications with token: [redacted]
<Notice>: Registering tag: user:[redacted]

日志中根本没有“错误注册标签”或“更新标签被取消”的实例,这告诉我方法调用成功无一例外。但是,当我尝试向空白/空标签或我的测试用户的特定标签发送测试通知时,不会收到任何通知,并且消息仅显示“消息已成功发送,但没有匹配的目标”。

另外,当我使用 var registrations = await hub.GetAllRegistrationsAsync(0); 提取所有注册时,我只看到我在 Android 方面的成功测试中的 FCM(Firebase/Android)注册。

我完全不知所措并且碰壁了,因为没有抛出异常,而且似乎没有办法解决幕后发生的事情。

这也是我的第二次尝试 - 我使用了更复杂的 SBNotificationHub 实现并且得到了相同的结果 - 没有例外,一切看起来都很好。

【问题讨论】:

  • Here 是一个类似的线程。您可以尝试答案中的步骤。

标签: xamarin.forms xamarin.ios apple-push-notifications azure-notificationhub


【解决方案1】:

感谢指向另一个问题的评论,我确定我需要做的就是确保我的标签注册在主 UI 线程上运行。我下面的更新代码正在运行:

    public async Task UpdateTags(string token)
    {
        await Task.Run(() =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    // No point registering tags until the user has signed in and we have a device token
                    if (CurrentAccount == null)
                    {
                        Console.WriteLine($"UpdateTags cancelled: Account: {Trico.OrbitalApp.App.CurrentAccount};");
                        return;
                    }
                    var tag = $"user:{CurrentAccount.UserName}";
                    Console.WriteLine($"Registering tag: {tag}");
                    MSNotificationHub.AddTag(tag);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error registering device: {e.ToString()}");
                }
            });
        });
    }

【讨论】:

    【解决方案2】:

    您可以尝试实现MSInstallationLifecycleDelegate 接口,这将允许您检查安装是否保存在后端,无论成功还是失败。

    // Set a listener for lifecycle management
    MSNotificationHub.SetLifecycleDelegate(new InstallationLifecycleDelegate());
    
    // Implementation of the lifecycle listener.
    public class InstallationLifecycleDelegate : MSInstallationLifecycleDelegate
    {
        public InstallationLifecycleDelegate()
        {
        }
    
        public override void DidFailToSaveInstallation(MSNotificationHub notificationHub, MSInstallation installation, NSError error)
        {
            Console.WriteLine($"Save installation failed with exception: {error.LocalizedDescription}");
        }
    
        public override void DidSaveInstallation(MSNotificationHub notificationHub, MSInstallation installation)
        {
            Console.WriteLine($"Installation successfully saved with Installation ID: {installation.InstallationId}");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 2018-05-06
      • 2018-04-28
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2019-06-18
      相关资源
      最近更新 更多