Windows Phone 8通知和后台任务



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

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

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

我认为"原始通知">是正确的选择,因为我不需要更新应用程序互动程序,甚至不需要显示Toast通知。但是,我还没有找到一个例子,或者在文件中,如果我能做到这一点。

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

我已经检查了另一个线程,

Windows Phone 8后台任务与通知

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

问题的答案并不完全是"否",你是对的whatsapp为此使用了破解,whatsapp有些人会使用AudioAgent,因为他们可以在后台运行,

我不知道他们到底是怎么做的,我也在寻找同样的答案,让我们看看我是否发现了一些东西会在这里发布

这在Windows Phone 8.1中发生了更改。从原始通知概述(Windows运行时应用程序)

接收原始通知

有两种途径可以让您的应用程序接收原始数据通知:

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

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

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

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

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

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

确保您通过记录它来获得URI。保存URI并在每次应用程序启动时运行此操作,因为URI会频繁更新。

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

  2. 在新创建的项目中创建一个扩展IBackgroundTask的新类。我叫我的"通知接收者":

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

您的任务将在"Run"函数中执行。

  1. 在主项目中引用运行时组件:点击你的Windows Phone项目->右键点击"引用"->添加引用->勾选你的运行时组件并按OK

  2. 编辑您的应用程序清单:双击您的包清单->声明->将"位置"one_answers"推送通知"添加到支持的任务类型,将您的后台任务类名添加到入口点:我的名为"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;
    

最新更新