简体   繁体   English

iOS 使用 Azure 通知中心推送通知

[英]iOS Push Notifications with Azure Notification Hub

I am having absolutely no luck getting push notifications to work in iOS in a Xamarin Forms project.我绝对没有运气在 Xamarin Forms 项目中的 iOS 中工作。

In AppDelegate.cs, I am calling the following in the FinishedLaunching override:在 AppDelegate.cs 中,我在 FinishedLaunching 覆盖中调用以下内容:

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

After the user logs in further in the app lifecycle, I also register the user with their user tag as follows:用户在应用程序生命周期中进一步登录后,我还使用他们的用户标签注册用户,如下所示:

    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()}");
            }
        });
    }

I have properly configured the Apple (APNS) settings in the notification hub, using the Token authentication mode (verified the four fields several times).我已经在通知中心正确配置了 Apple (APNS) 设置,使用 Token 身份验证模式(多次验证了四个字段)。 The certificate (signing identity) is "iOS Distribution", the identifier bundle matches exactly what I have in the configuration (not using wildcard), the key has Apple Push Notifications service (APNs) enabled, and the provisioning profile has Platform: iOS and Type: App Store.证书(签名身份)是“iOS Distribution”,标识符包与我在配置中的完全匹配(不使用通配符),密钥启用了 Apple Push Notifications 服务(APNs),并且配置文件具有平台:iOS 和类型:应用商店。

I pushed the application to TestFlight, as I don't have access to a physical Mac (we use a Cloud mac for development).我将应用程序推送到 TestFlight,因为我无法访问物理 Mac(我们使用 Cloud mac 进行开发)。 When I view the device logs from my personal iPhone with the app installed, I see the following when I run it:当我从安装了该应用程序的个人 iPhone 查看设备日志时,我在运行它时看到以下内容:

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

There are no instances of "Error registering tag" or "UpdateTags cancelled" in the logs at all, which tells me that the method calls are succeeding without an exception.日志中根本没有“错误注册标签”或“更新标签已取消”的实例,这告诉我方法调用成功无异常。 However, when I attempt to send a test notification to either a blank/empty tag, or the specific tag for my test user, no notifications are received and the messaging simply shows "Message was successfully sent, but there were no matching targets."但是,当我尝试向空白/空标签或我的测试用户的特定标签发送测试通知时,不会收到任何通知,并且消息仅显示“消息已成功发送,但没有匹配的目标”。

Also, when I pull all of the registrations with var registrations = await hub.GetAllRegistrationsAsync(0);此外,当我使用var registrations = await hub.GetAllRegistrationsAsync(0); , I only see the FCM (Firebase/Android) registrations from my successful testing on the Android side of things. ,我只在 Android 方面的成功测试中看到 FCM(Firebase/Android)注册。

I am at a complete loss and have hit a wall, as there are no exceptions being thrown, and seemingly no way to troubleshoot what is going on behind the scenes.我完全不知所措并且碰壁了,因为没有抛出异常,而且似乎没有办法解决幕后发生的事情。

This is also my 2nd attempt - I was using a more complex SBNotificationHub implementation and had the same results - no exceptions and everything looked fine at face value.这也是我的第二次尝试——我使用了更复杂的 SBNotificationHub 实现并得到了相同的结果——没有例外,一切看起来都很好。

You can try implementing the MSInstallationLifecycleDelegate interface which will allow you to check and see if the installation is being saved on the back end with either success or failure.您可以尝试实现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}");
    }
}

Thanks to a comment pointing to another question, I have determined that all I needed to do was to ensure that my tag registration ran on the main UI thread.感谢指向另一个问题的评论,我确定我需要做的就是确保我的标签注册在主 UI 线程上运行。 My updated code below is working:我下面的更新代码正在运行:

    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()}");
                }
            });
        });
    }

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

相关问题 从Azure通知中心(Xamarin.iOS)推送通知 - Push notifications from Azure Notification Hub (Xamarin.iOS) Xamarin.Android将通知GCM推送到Azure通知中心 - Xamarin.Android push notifications GCM to Azure Notification Hub 为Azure通知中心推送通知 - Push notification for azure notification hub 使用Azure Notification Hub在Xamarin Forms上注册Android设备以进行推送通知 - Registering an Android device for push notifications on Xamarin Forms using Azure Notification Hub 标签未在Azure推送通知中心注册 - Tags are not registered in Azure push notifications hub Xamarin.iOS推送通知标记.NET API / Azure通知中心 - Xamarin.iOS push notification tag .NET API / Azure Notification Hub 我们如何触发 Azure 通知中心从 .net Z2567A5EC9305EB7AC182C4EBZ 应用程序向 iOS 设备发送推送通知? - How we can trigger Azure Notification Hub to send push notification to iOS devices from .net web application? Xamarin Forms IOS 与 Azure 通知中心 - Xamarin Forms IOS with Azure Notification Hub 设备注册后将推送通知标签添加到Azure通知中心 - Adding push notification tags to Azure Notification Hub after device registration 如何使用 SendNotificationAsync 方法将 ios 静默推送通知发送到通知中心 - How to send ios silent push notification to Notification Hub with SendNotificationAsync method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM