Windows Phone 8 notifications and background tasks

坚强是说给别人听的谎言 提交于 2019-11-29 11:15:34

This has changed in Windows Phone 8.1. From Raw notification overview (Windows Runtime apps)

Receiving a raw notification

There are two avenues through which your app can be receive raw notifications:

  • Through notification delivery events while your application is running.
  • Through background tasks triggered by the raw notification if your app is enabled to run background tasks.

An app can use both mechanisms to receive raw notifications. If an app implements both the notification delivery event handler and background tasks that are triggered by raw notifications, the notification delivery event will take priority when the app is running.

  • If the app is running, the notification delivery event will take priority over the background task and the app will have the first opportunity to process the notification.
  • The notification delivery event handler can specify, by setting the event's PushNotificationReceivedEventArgs.Cancel property to true, that the raw notification should not be passed to its background task once the handler exits. If the Cancel property is set to false or is not set (the default value is false), the raw notification will trigger the background task after the notification delivery event handler has done its work.

Answer to your question is not exactly "NO", and you are right whatsapp uses hack for this, whatsapp someyow use AudioAgent, as they are allowed to run in background,

i dont know how exactly they do it, i am also searching for the same answer, let's see if i find something will post here

Here is a complete guide on receiving push notifications in the background for Windows Phone 8.1:

  1. Get a push notifications channel URI:

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

Make sure you actually get the URI by logging it. Save the URI and run this on every app launch as the URI gets updated quite frequently.

  1. Create a Windows Runtime Component project inside your solution: Right click on solution -> Add -> New Project -> select "Windows Runtime Component (Windows Phone)". Call this project "Tasks" or whatever you prefer.

  2. Create a new class extending an IBackgroundTask inside your newly created project. I called mine "NotificationReceiver":

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

Your task will be implemented here inside "Run" function.

  1. Reference your Runtime Component in your main project: Click on your Windows Phone project -> right click on "References" -> Add Reference -> Tick your Runtime Component and press OK.

  2. Edit your app manifest: Double-click on your package manifest -> Declarations -> add "Location" and "Push notification" to Supported task types, add your background task class name to Entry point: mine is called "Tasks.NotificationReceiver". Save your changes.

  3. Unregister and register your background task every time the app is launched. I am giving the complete solution, just call "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. Get RawNotification object inside your task:

    RawNotification notification = (RawNotification) taskInstance.TriggerDetails;
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!