Windows Phone 8通知和后台任务

我搜索了官方论坛和文档以及所有其他地方,但未能找到具体的答案。

问:在Windows Phone 8中,应用程序是否可以响应推送通知,并在后台执行任务?

据我所知,对于Toast和Tile Notifications ,当应用程序不在前台时,根本没有钩子,因为它能够响应传入的消息。

我认为“原始通知”是正确的选择,因为我不需要更新应用程序磁贴,甚至不显示Toast Notification。 但是,如果我能做到这一点,我无法找到一个例子,或者在文件中。

我找到了几个链接,讨论为Windows商店应用程序执行此操作,但我想知道是否可以为Windows Phone 8完成此操作。

我检查了这个其他post,

带有通知的Windows Phone 8后台任务

其中一个答案表明Whatsapp实际上有一个黑客,在收到推送通知后下载消息。 那么,是我的问题的答案,不是吗?

这在Windows Phone 8.1中已更改。 来自Raw通知概述(Windows运行时应用程序)

收到原始通知

您的应用可以通过两种方式接收原始通知:

  • 在应用程序运行时通过通知传递事件。
  • 如果您的应用程序已启用运行后台任务,则通过原始通知触发后台任务。

应用程序可以使用这两种机制来接收原始通知。 如果应用程序同时实现通知传递事件处理程序和原始通知触发的后台任务,则通知传递事件将在应用程序运行时优先。

  • 如果应用程序正在运行,则通知传递事件将优先于后台任务,应用程序将首次有机会处理通知。
  • 通知传递事件处理程序可以通过将事件的PushNotificationReceivedEventArgs.Cancel属性设置为true来指定在处理程序退出后不应将原始通知传递给其后台任务。 如果Cancel属性设置为false或未设置(默认值为false),则原始通知将在通知传递事件处理程序完成其工作后触发后台任务。

回答你的问题不完全是“不”,你是对的whatsapp使用hack为此,whatsapp某些人使用AudioAgent,因为他们被允许在后台运行,

我不知道他们究竟是怎么做的,我也在寻找相同的答案,让我们看看我是否会发现这里会发布的内容

以下是在Windows Phone 8.1后台接收推送通知的完整指南:

  1. 获取推送通知渠道URI:

    PushNotificationChannel _channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); string ChannelUri = _channel.Uri; 

确保通过记录它来实际获取URI。 保存URI并在每次应用启动时运行此URI,因为URI经常更新。

  1. 在解决方案中创建Windows运行时组件项目:右键单击解决方案 – >添加 – >新建项目 – >选择“Windows运行时组件(Windows Phone)”。 将此项目称为“任务”或您喜欢的任何内容。

  2. 在新创建的项目中创建一个扩展IBackgroundTask的新类。 我称之为“NotificationReceiver”:

     using Windows.ApplicationModel.Background; namespace Tasks { public sealed class NotificationReceiver : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { // TODO: implement your task here } } } 

您的任务将在“运行”function中实现。

  1. 在主项目中引用运行时组件:单击Windows Phone项目 – >右键单击“引用” – >“添加引用” – >勾选“运行时组件”,然后按“确定”。

  2. 编辑应用清单:双击包清单 – >声明 – >将“位置”和“推送通知”添加到支持的任务类型,将后台任务类名称添加到入口点:我的名称为“Tasks.NotificationReceiver”。 保存更改。

  3. 每次启动应用程序时取消注册并注册后台任务。 我给出了完整的解决方案,只需调用“setupBackgroundTask()”:

     private void setupBackgroundTask() { requestAccess(); UnregisterBackgroundTask(); RegisterBackgroundTask(); } private void RegisterBackgroundTask() { BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder(); PushNotificationTrigger trigger = new PushNotificationTrigger(); taskBuilder.SetTrigger(trigger); taskBuilder.TaskEntryPoint = "Tasks.NotificationReceiver"; taskBuilder.Name = "pushTask"; try { BackgroundTaskRegistration task = taskBuilder.Register(); Logger.log("TASK REGISTERED"); } catch (Exception ex) { Logger.log("FAILED TO REGISTER TASK"); UnregisterBackgroundTask(); } } private bool UnregisterBackgroundTask() { foreach (var iter in BackgroundTaskRegistration.AllTasks) { IBackgroundTaskRegistration task = iter.Value; if (task.Name == "pushTask") { task.Unregister(true); Logger.log("TASK UNREGISTERED"); return true; } } return false; } private async void requestAccess() { BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync(); } 
  4. 在任务中获取RawNotification对象:

     RawNotification notification = (RawNotification) taskInstance.TriggerDetails;