Xamarin.forms 使用Azure通知中心的iOS推送通知

Xamarin.forms 使用Azure通知中心的iOS推送通知,xamarin.forms,xamarin.ios,apple-push-notifications,azure-notificationhub,Xamarin.forms,Xamarin.ios,Apple Push Notifications,Azure Notificationhub,我在Xamarin Forms项目的iOS中完全没有得到推送通知的机会 在AppDelegate.cs中,我在FinishedLaunching覆盖中调用以下命令: MSNotificationHub.Start("Endpoint=sb://[redacted].servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=[redacted]",

我在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 Push Notifications service(APNs),并且配置文件具有Platform:iOS和Type:App Store

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

<Notice>: Registered for push notifications with token: [redacted]
<Notice>: Registering tag: user:[redacted]
:使用令牌注册推送通知:[已编辑]
:注册标签:用户:[已编辑]
日志中根本没有“errorregistingtag”或“updatetagsincelled”的实例,这告诉我方法调用是成功的,没有异常。但是,当我尝试向空白/空标记或测试用户的特定标记发送测试通知时,不会收到任何通知,消息仅显示“消息已成功发送,但没有匹配的目标”

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

我完全不知所措,已经碰壁了,因为没有任何异常被抛出,而且似乎没有办法解决幕后发生的问题


这也是我的第二次尝试-我使用了更复杂的SBNotificationHub实现,结果相同-没有异常,从表面上看一切正常。

您可以尝试实现
MSInstallationLifecycleDelegate
接口,该接口将允许您检查并查看安装是否保存在服务器上后端要么成功,要么失败

//为生命周期管理设置侦听器
MSNotificationHub.SetLifecycleDelegate(新安装LifecycleDelegate());
//生命周期侦听器的实现。
公共类InstallationLifecycleLegate:MSInstallationLifecycleLegate
{
公共安装LifecycleLegate()
{
}
公共覆盖void didtoSaveInstallation失败(MSNotificationHub notificationHub,MSInstallation installation,NSError错误)
{
Console.WriteLine($“保存安装失败,异常:{error.LocalizedDescription}”);
}
公共覆盖void didsave安装(MSNotificationHub notificationHub,MSInstallation安装)
{
Console.WriteLine($“安装已成功保存,安装ID为:{Installation.InstallationId}”);
}
}

多亏了一条指向另一个问题的评论,我已经确定我需要做的就是确保我的标记注册在主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()}");
                }
            });
        });
    }
是一条类似的线。你可以试试答案中的步骤。