I have searched the official forums and documentation and all other places, but haven't been able to find a concrete answer.
Q. In Windows Phone 8, is it possible for an application to respond to a Push Notification, and perform a task, while it is in the background?
As far as I can understand, for Toast and Tile Notifications, when the app is not in the foreground, there are no hooks at all for it to be able to respond to the incoming message.
I think "raw notifications" are the correct choice for this, since I am not required to update the app tile, or even show a Toast Notification. But, I haven't been able to find an example, or in the documentations, if I can do this.
I have found several links which talk about doing this for Windows store apps, but I want to find out if this can be done for Windows Phone 8.
I have checked this other thread,
Windows Phone 8 Background Task with notifications
Where one of the answer suggests that Whatsapp actually has a hack for this, to download the messages after a push notification is received. So, is the answer to my question, a NO?
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:
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.
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.
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.
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.
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.
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(); }
Get RawNotification object inside your task:
RawNotification notification = (RawNotification) taskInstance.TriggerDetails;
来源:https://stackoverflow.com/questions/21084955/windows-phone-8-notifications-and-background-tasks