OGeek|极客世界-中国程序员成长平台

标题: ios - Azure 通知中心 Xamarin 为多个模板注册多个 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:39
标题: ios - Azure 通知中心 Xamarin 为多个模板注册多个

我需要一些帮助来为 Azure 通知中心的多个模板注册 iOS 设备。

注册单个模板工作正常,但似乎在从单个设备注册多个模板时,注册的第二个模板总是工作正常,而注册的第一个模板无法正常工作。

前几天我发现一些东西说每个模板都必须有一个唯一的 PNS 句柄,但即使获得(我认为可能是)2 个独特的 PNS 句柄似乎也不起作用。

模板#1:

{"aps":{"title":"$(emergencyTitle)","alert":"$(emergencyMessage)","tags":"$(emergencyTags)"}}

模板#2:

{"aps":{"content-available":1,"title":"$(regularTitle)","alert":"$(regularMessage)","inAppMessage":"$(regularMessage)","tags":"$(regularTags)"}}

下面的代码被调用了两次(我希望它会得到 2 个独特的 PNS 句柄)。每次,它都会将 deviceToken 保存在一个变量中,以便我可以向它们注册 2 个模板:

if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) {
    UIApplication.SharedApplication.RegisterUserNotificationSettings(UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new Foundation.NSSet()));

    UIApplication.SharedApplication.RegisterForRemoteNotifications();
} else {
    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);
}

然后以下精简代码调用通知中心以尝试注册 2 个模板:

Hub = Hub ?? new WindowsAzure.Messaging.SBNotificationHub(Constants.ConnectionString, Constants.NotificationHubPath);

// App.PushNotificationPnsHandles is a Dictionary<string, object> that holds an 'emergency' or 'regular' key and the deviceTokens
// Constants.EmergencyNotificationTemplate and Constants.RegularNotificationTemplate hold the template strings

Hub.UnregisterAllAsync((NSData)App.PushNotificationPnsHandles["emergency"], error => {
    Hub.RegisterTemplateAsync((NSData)App.PushNotificationPnsHandles["emergency"], "EmergencyTemplate", Constants.EmergencyNotificationTemplate, System.DateTime.Now.AddDays(90).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US")), new NSSet(), errorCallback => { });
});

Hub.UnregisterAllAsync((NSData)App.PushNotificationPnsHandles["regular"], error => {
    Hub.RegisterTemplateAsync((NSData)App.PushNotificationPnsHandles["regular"], "RegularTemplate", Constants.RegularNotificationTemplate, System.DateTime.Now.AddDays(90).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US")), new NSSet(), errorCallback => { });
});

同样,如果我打一个电话来注册紧急模板,一切都会很好。如果我打一个电话来注册常规模板,一切都会很好。

如果我尝试先注册紧急模板,然后再注册常规模板,然后我发送紧急推送通知,推送通知确实到达手机但它不包含任何变量中的文本......不确定我在这里做错了什么。谢谢。

编辑:查看 Visual Studio 中的通知中心调试窗口,我可以看到 2 个注册,每个都有不同的注册 ID 但具有相同的 PNS 标识符。如果是这种情况,那么我不确定如何为每个模板获取唯一标识符。



Best Answer-推荐答案


  1. 特定设备上的特定应用由一个特定的 PNS 句柄标识。同一设备上的同一应用不能有多个不同的 PNS 句柄。
  2. 如果您不想实际删除所有注册,请不要使用 UnregisterAllAsync()。只需使用 RegisterTemplateAsync(),它将创建或更新模板注册。
  3. 如果您想使用多个模板,您必须区分具有不同标签的注册,而不是使用不同的 PNS 句柄,因为 PNS 句柄始终相同。

那么,你要做的是:

  1. 不要两次获得 PNS 句柄,而只能一次,因为无论如何它总是相同的。
  2. 删除对 UnregisterAllAsync() 的调用,RegisterTemplateAsync() 就足够了。
  3. 将至少一个标签传递给 RegisterTemplateAsync() 而不是空的 NSSet,并确保每个模板注册的标签都不同,例如“模板:紧急”和“模板:常规”。
  4. 如果您想发送“常规”通知,请提供“template:regular”标签以解决正确的注册问题,如果您想发送“紧急”通知,请使用“template:emergency”标签。<

关于ios - Azure 通知中心 Xamarin 为多个模板注册多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792852/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4