0

I'm having the following problem with UWP push notifications.

Scenario:

  1. Foreground registers a background task for push notifications but also wants to be notified of incoming pushes so it gets a channel, updates the server with the channel then hooks onto events on that channel.

  2. Push notification comes in

  3. Background task launches and does it's work. It also checks to see if the channel URI has changed, if it has, it updates the server with the new uri.

Result:

Foreground app no longer receives incoming pushes on the channel because the background task updated the server with a different channel uri from what the foreground app was listening on.

What is the proper way for the foreground app to continue listening for push notifications after the uri potentially changed?


Updated problem for clarification:

Step 1 Foreground app code:

var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += OnPushNotificationReceived;
//<update server with channel.Uri

Step 2 Background task launches and also checks for a new channel uri:

var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
//update server with channel uri

Problem

If channel uri changes in Step 2 then the even handler in Step 1 never gets invoked again.

Justin Horst
  • 201
  • 1
  • 9

1 Answers1

0

because the background task updated the server with a different channel uri from what the foreground app was listening on.

No. Your understanding was incorrect. Let's first check the steps on how to send a push notification:

  1. Your app requests a push notification channel from the Universal Windows Platform.
  2. Windows asks WNS to create a notification channel. This channel is returned to the calling device in the form of a Uniform Resource Identifier (URI).
  3. The notification channel URI is returned by Windows to your app.
  4. Your app sends the URI to your own cloud service. You then store the URI on your own cloud service so that you can access the URI when you send notifications.
  5. When your cloud service has an update to send, it notifies WNS using the channel URI. This is done by issuing an HTTP POST request, including the notification payload, over Secure Sockets Layer (SSL). This step requires authentication.
  6. WNS receives the request and routes the notification to the appropriate device.

enter image description here

Then, if you understand these steps, you will find that the key point of sending a notification to the appropriate device is you need to request a notification channel. This channel URI is unique for every device no matter whether the channel URI is updated. So, even if your UWP app's background task has updated the channel URI on your cloud service, it will not affect the device to receive the notification. As long as you could make sure that the channel URI is the latest when you use this channel URI to send notification, then your device will always receive the notification(Except for some special cases, e.g, the device is powered off or device is in battery saver etc).

Please read Windows Push Notification Services (WNS) overview for more detailed information.

What is the proper way for the foreground app to continue listening for push notifications after the uri potentially changed?

What you said was not correct. There is no relationship between the "listening for push notifications" and "the uri potentially changed".

If you want to do some operations when the devices receive notifications, you could use the Notification listener to perform specific actions when notifications are received.

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
  • Thanks for the response @Xavier Xie. Our app is currently doing this. We have no issues receiving push. Let me clarify my problem. In the foreground app we are calling: PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); and then: channel.PushNotificationReceived += OnPushNotificationReceived; The issue is when the channel URI changes (and we update the server with the new channel URI) that event callback in our foreground app will never get triggered again – Justin Horst Oct 16 '17 at 18:33
  • @JustinHorst Why not use [Notification listener](https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications/notification-listener)? If you use Notification listener, then you do not need to think about if the channel URI is updated. – Xie Steven Oct 17 '17 at 01:28
  • Xie - if I understand correctly notification listener requires us to get access to all of the user's notifications which we don't need, we need to be notified only when a raw notification comes in for our app – Justin Horst Oct 17 '17 at 17:47
  • @JustinHorst If so, I think you could register a new background task with `PushNotificationTrigger`, the task will run when a raw notification is received. Then, you do not need to carry about if the channel URI is changed. See this sample for reference:https://code.msdn.microsoft.com/windowsapps/Raw-notifications-sample-3bc28c5d/sourcecode?fileId=50986&pathId=1497403672 – Xie Steven Oct 18 '17 at 01:38