Reputation: 63
I searched a lot but I didn't get any documentation or example that can help me to use PushNotificationTrigger in Background Task like we use SystemTrigger and TimeTrigger.
I want to receive the Toast Notifications when my app is closed. Notifications are coming to windows but don't know how to handle that in background. Anybody who had worked on PushNotifications in Universal windows app please provide some sample or document to handle the notifications.
This is I am getting in Lifecycle Events This is the code I am using to register the Task
if(!Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name == "PushBackgroundTask"))
{
var result = await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "PushBackgroundTask";
builder.TaskEntryPoint = typeof(NotificationActionBackgroundTask.NotificationActionBackgroundTask).FullName;
builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());
BackgroundTaskRegistration task = builder.Register();
}
Thanks
Upvotes: 1
Views: 1029
Reputation: 16652
I think you may be a little confused here. When using PushNotification
, the server can push 4 kinds of notification to your device: Toast, Tile, Badge and Raw Notification.
The front three notifications will be received by Notification center and handled by system. Raw Notification is a push notification that does not involve UI, its contents can be used in an app's background task.
I want to receive the Toast Notifications when my app is closed.
So no, if you want to in background task handle the PushNotifications using PushNotificationTrigger
, you can only handle the Row Notifications. You can refer to PushNotificationTrigger class, this class represents an object that invokes a background work item on the app in response to the receipt of a raw notification.
Unlike other push notifications, raw notifications don't have a specified format. The content of the payload is entirely app-defined.
So if your push a toast notification, you don't need to handle it in your background task, it will be handled by action center. But you can handle a Raw Notification in your background task and toast it manually.
For the information about how to use PushNotificaitonTrigger
with Raw notification
, you can refer to Raw notification overview, and for toast in the background task, you can refer to the official Notifications sample.
Update
To debug the project with lifecycle events, you can take a look at the following screen shot:
When debuging, you apps lifecycle is depended on the vs tool, but you can click this red circle part to debug your app's lifecycle.
Upvotes: 2