- 1 year ago
Push Notifications for Windows 10 UWP hybrid mobile app
To implement push notifications for a Windows 10 UWP hybrid mobile app, you'll need to use the Windows Push Notification Services (WNS) provided by Microsoft. Here's a step-by-step guide on how to set up push notifications for your UWP hybrid app:
Register your app with the Windows Dev Center:
- Go to the Windows Dev Center (https://developer.microsoft.com/en-us/windows/), sign in with your Microsoft account, and register your app.
Obtain the Package SID and Client Secret:
- After registering your app, navigate to the "Services" section in the Dev Center and select "Push notifications." Here, you'll find your Package SID and Client Secret. Note down these values as you'll need them later.
Configure your UWP hybrid app for push notifications:
- In your UWP hybrid app project, open the
Package.appxmanifest
file. - Go to the "Packaging" tab and make sure that "Internet (Client)" capability is checked. This is required for push notifications.
- In your UWP hybrid app project, open the
Implement push notification handling in your app:
- In your app's JavaScript code, add the necessary code to handle push notifications. Here's a basic example:
javascript// Request push notification permission
Windows.Networking.PushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync()
.done(function (channel) {
// Channel URI is the unique identifier for your device to receive push notifications
var channelUri = channel.uri;
console.log('Channel URI: ' + channelUri);
// Send the channelUri to your server to associate it with the user's account
// Your server can use this channelUri to send push notifications to the app
}, function (error) {
console.error('Error getting push notification channel: ' + error.message);
});
- Send push notifications from your server:
- Your server will need to use the Package SID and Client Secret obtained from the Windows Dev Center to authenticate with WNS and send push notifications to the app.
- You can use a server-side library or the WNS REST API to send push notifications. Refer to the Microsoft documentation for more details on how to send push notifications: https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=javascript
Please note that to implement push notifications, you'll need to have a server component to handle sending push notifications to WNS. The client-side code in your UWP hybrid app will obtain the push notification channel URI and send it to your server, and then your server will use this channel URI to send push notifications to the app.
Also, keep in mind that push notifications may not work when running your app locally in development, as it requires a valid app package from the Windows Store and a valid SID and secret obtained from the Dev Center. To fully test push notifications, you'll need to deploy your app to a real Windows 10 device or submit it to the Windows Store for testing.