如何在应用程序中关闭旧的推送通知通道?
How to close old PushNotificationChannel in Application?
我在存储 PushNotificationChannel 时遇到了很大的问题。类型本身不能存储在 LocalSettings 中。
我需要它来在获得新频道后关闭频道。每次启动应用程序时我都会请求新频道(如文档所述),有时我会获得新频道(不同于以前的频道),而以前的 stil 有效(导致重复的推送通知)。因此,当我获得新频道时,我想关闭旧频道。
我还尝试使用以下方式将其序列化为流式传输:
var serializer = new DataContractSerializer(typeof(PushNotificationChannel));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, channel);
但是我得到错误:
'Windows.Networking.PushNotifications.PushNotificationChannel' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
有没有办法存储 PushNotificationChannel 或者我可以只存储 URI 并以某种方式从 uri 创建 Channel 对象?
编辑:
好的,我可以在我的 PushNotificationReceived 中获取 Channel。但我不知道如何在我的 BackgroundTask 中获取发件人 PushNotificationChannel - 这可能吗?
是的,您可以在后台获取 PushNotificationChannel。您需要为此创建后台任务并在后台任务中点击 API 以获取推送通知通道 Uri。用于在 windows 商店应用 see this tutorial.
中创建后台任务
我在存储 PushNotificationChannel 时遇到了很大的问题。类型本身不能存储在 LocalSettings 中。
我需要它来在获得新频道后关闭频道。每次启动应用程序时我都会请求新频道(如文档所述),有时我会获得新频道(不同于以前的频道),而以前的 stil 有效(导致重复的推送通知)。因此,当我获得新频道时,我想关闭旧频道。
我还尝试使用以下方式将其序列化为流式传输:
var serializer = new DataContractSerializer(typeof(PushNotificationChannel));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, channel);
但是我得到错误:
'Windows.Networking.PushNotifications.PushNotificationChannel' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
有没有办法存储 PushNotificationChannel 或者我可以只存储 URI 并以某种方式从 uri 创建 Channel 对象?
编辑:
好的,我可以在我的 PushNotificationReceived 中获取 Channel。但我不知道如何在我的 BackgroundTask 中获取发件人 PushNotificationChannel - 这可能吗?
是的,您可以在后台获取 PushNotificationChannel。您需要为此创建后台任务并在后台任务中点击 API 以获取推送通知通道 Uri。用于在 windows 商店应用 see this tutorial.
中创建后台任务