hva*_*an3 3 templates azure ios xamarin azure-notificationhub
我需要一些帮助来使用 Azure 通知中心为多个模板注册 iOS 设备。
注册单个模板工作正常,但似乎从单个设备注册多个模板时,注册的第二个模板始终正常工作,而注册的第一个模板无法正常工作。
前几天我发现一些东西说每个模板都必须有一个独特的 PNS 句柄,但即使获得(我认为可能是)2 个独特的 PNS 句柄似乎也不起作用。
模板#1:
{"aps":{"title":"$(emergencyTitle)","alert":"$(emergencyMessage)","tags":"$(emergencyTags)"}}
Run Code Online (Sandbox Code Playgroud)
模板#2:
{"aps":{"content-available":1,"title":"$(regularTitle)","alert":"$(regularMessage)","inAppMessage":"$(regularMessage)","tags":"$(regularTags)"}}
Run Code Online (Sandbox Code Playgroud)
下面的代码被调用两次(我希望能得到 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);
}
Run Code Online (Sandbox Code Playgroud)
然后,以下精简代码调用通知中心以尝试注册 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 => { });
});
Run Code Online (Sandbox Code Playgroud)
同样,如果我打一个电话来注册紧急模板,一切都很好。如果我打一个电话来注册常规模板,一切都很好。
如果我尝试先注册紧急模板,然后注册常规模板,然后我发送紧急推送通知,推送通知确实到达手机,但它不包含任何变量中的文本......不知道我是什么我在这里做错了。谢谢。
编辑:查看 Visual Studio 中的通知中心调试窗口,我可以看到 2 个注册,每个注册具有不同的注册 ID,但具有相同的 PNS 标识符。如果是这种情况,那么我不确定如何为每个模板获取唯一标识符。
UnregisterAllAsync()如果您不想实际删除所有注册,请不要使用。只需使用RegisterTemplateAsync(),它就会创建或更新模板注册。所以,你需要做的是:
UnregisterAllAsync(),RegisterTemplateAsync()就足够了。RegisterTemplateAsync()而不是空的NSSet,并确保每个模板注册的标签都不同,例如“template:emergency”和“template:regular”。| 归档时间: |
|
| 查看次数: |
838 次 |
| 最近记录: |