ios - Azure 通知中心 Xamarin 为多个模板注册多个
<p><p>我需要一些帮助来为 Azure 通知中心的多个模板注册 iOS 设备。</p>
<p>注册单个模板工作正常,但似乎在从单个设备注册多个模板时,注册的第二个模板总是工作正常,而注册的第一个模板无法正常工作。</p >
<p>前几天我发现一些东西说每个模板都必须有一个唯一的 PNS 句柄,但即使获得(我认为可能是)2 个独特的 PNS 句柄似乎也不起作用。</p>
<p>模板#1:</p>
<pre><code>{"aps":{"title":"$(emergencyTitle)","alert":"$(emergencyMessage)","tags":"$(emergencyTags)"}}
</code></pre>
<p>模板#2:</p>
<pre><code>{"aps":{"content-available":1,"title":"$(regularTitle)","alert":"$(regularMessage)","inAppMessage":"$(regularMessage)","tags":"$(regularTags)"}}
</code></pre>
<p>下面的代码被调用了两次(我希望它会得到 2 个独特的 PNS 句柄)。每次,它都会将 <code>deviceToken</code> 保存在一个变量中,以便我可以向它们注册 2 个模板:</p>
<pre><code>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);
}
</code></pre>
<p>然后以下精简代码调用通知中心以尝试注册 2 个模板:</p>
<pre><code>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 => { });
});
</code></pre>
<p>同样,如果我打一个电话来注册紧急模板,一切都会很好。如果我打一个电话来注册常规模板,一切都会很好。</p>
<p>如果我尝试先注册紧急模板,然后再注册常规模板,然后我发送紧急推送通知,推送通知确实到达手机但它不包含任何变量中的文本......不确定我在这里做错了什么。谢谢。</p>
<p>编辑:查看 Visual Studio 中的通知中心调试窗口,我可以看到 2 个注册,每个都有不同的注册 ID 但具有相同的 PNS 标识符。如果是这种情况,那么我不确定如何为每个模板获取唯一标识符。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><ol>
<li>特定设备上的特定应用由一个特定的 PNS 句柄标识。同一设备上的同一应用不能有多个不同的 PNS 句柄。</li>
<li>如果您不想实际删除所有注册,请不要使用 <code>UnregisterAllAsync()</code>。只需使用 <code>RegisterTemplateAsync()</code>,它将创建<strong>或更新</strong>模板注册。</li>
<li>如果您想使用多个模板,您必须<strong>区分具有不同标签的注册</strong>,而不是使用不同的 PNS 句柄,因为 PNS 句柄始终相同。</li>
</ol>
<p>那么,你要做的是:</p>
<ol>
<li>不要两次获得 PNS 句柄,而只能一次,因为无论如何它总是相同的。</li>
<li>删除对 <code>UnregisterAllAsync()</code> 的调用,<code>RegisterTemplateAsync()</code> 就足够了。</li>
<li>将至少一个标签传递给 <code>RegisterTemplateAsync()</code> 而不是空的 <code>NSSet</code>,并确保每个模板注册的标签都不同,例如“模板:紧急”和“模板:常规”。</li>
<li>如果您想发送“常规”通知,请提供“template:regular”标签以解决正确的注册问题,如果您想发送“紧急”通知,请使用“template:emergency”标签。<</li>
</ol></p>
<p style="font-size: 20px;">关于ios - Azure 通知中心 Xamarin 为多个模板注册多个,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34792852/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34792852/
</a>
</p>
页:
[1]