170 likes | 310 Views
Delivering push notifications to millions of mobile devices. Tamara Panova Developer DataArt. Why Notification Hubs?. Push is essential to the user experience of many apps. Increase user engagement. Update tiles/widgets with current financial/weather information.
E N D
Delivering push notifications to millions of mobile devices Tamara Panova Developer DataArt
Why Notification Hubs? Push is essential to the user experience of many apps. • Increase user engagement. • Update tiles/widgets with current financial/weather information. • Display badges with the number of current sales leads in a CRM app. Real world apps have complex needs. • Multi-platform push. • Localization. • User preferences. • Different client app versions. • Scale. Windows News app uses Notification Hubs
Push notifications • Push notifications require a platform specific service. • Each platform (Windows Store, iOS, Android, …) has a different push notification service. • Different capabilities and protocols. • An e2e solution requires lots of back-end code. • Store and keep up to date the device information. • Implement platform-specific protocols.
Push notification lifecycle • Registration at app launch. • Client app contacts Platform Notification Service, to retrieve current channel (e.g., ChannelURIs, device tokens, registrationIds). • App updates handle in back-end. • Sending Notification. • App back-end send notification to PNS. • PNS pushes the notification to the app on the device. • Maintenance. • Delete expired handles when PNS rejects them. Client app Platform Notification Service App back-end
Challenges of push notifications • Platform dependency • Different communication protocols to PNS’ (e.g., HTTP vs. TCP, xml payload vs. JSON payload). • Different presentation formats and capabilities (tiles vs. toasts vs. badges). • Routing • PNS’ provide a way to send a message to a device/channel. • Usually notifications are targeted at users or interest groups(e.g., employees assigned to a customer account). • App back-end has to maintain a registry associating device handles to interest groups/users. • Scale • App back-end has to store current handles for each device high storage and VM costs. • Broadcast to millions of devices with low latency requires parallelization (DB ad VM).
Using Notification Hubs • One-time set up • Create a Notification Hub in Service Bus. • Register • The client app retrieves its current handle from the PNS. • Client app creates (or updates) a registration on the Notification Hub with the current handle. • Send Notification • The app back-end sends a message to the Notification Hub. • Notification Hub pushes it to the PNS’. iOS app Windows Store app Notification Hub App back-end WNS APNs
Advantages of using Notification Hubs • No platform-specific protocols. • App back-end just communicates with the Notification Hub. • Avoid storing device information in the app back-end. • Notification Hub maintains the registry of devices and the associations to users/interest groups. • Broadcast • Push notifications to millions of devices (across platforms) with a single call.
Register a Windows Store app • varhub = newNotificationHub(“<hub name>", "<connection string>"); • var channel = awaitPushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); • awaithub.RegisterNativeAsync(channel.Uri);
Broadcast a Windows notification • varhubClient = NotificationHubClient.CreateClientFromConnectionString("<connection string>", “<hub name>"); • vartoast = @“<notification payload>"; • hubClient.SendWindowsNativeNotificationAsync(toast);
Take-aways • No need to store and maintain ChannelURIs. • In your device local storage, in the cloud. • Device registrations expire. • No need to clean-up when app is uninstalled. • Call RegisterAsync regularly.
Sending notifications to specific devices • Tags as interest groups. • Client app can register with a set of tags. • Tags are simple strings (no pre-provisioning is required). • App back-end can target all clients with the same tag. • You can use tags also for: • Multiple type of interest groups, e.g.,: • Follow bands: tag “followband:Beatles”. • Follow users: tag “followuser:Alice”. • Tag devices with a user ID. Tag:”Beatles” Notification Hub App back-end Tag:”Wailers” Tag:”Beatles”
Take-aways • Store the categories/tags. • In your device local storage, in the cloud. • Make sure to register regularly. • Rule of thumb: “every app start, up to once per day.”
Tags as user IDs • Registering from device is not secure. • Every device can register for any tag. • Embedding credentials in the device works for “public” notifications (e.g., News apps). • Register from back-end. • Device does *not* contain the notification hub credentials. • Devices authenticate with the app back-end to register. • App back-end registers the device for the correct tags. • Same registration patterns apply. • Devices have to register regularly (registrations still expire). • Store the required tags in your back-end. Notification Hub App back-end
Take-aways • When security is needed register from the back-end • No Notification Hub SDK required on the devices
Using templates for multi-platform push <toast> <visual> <binding template=\"ToastText01\"> <text id=\"1\">$(message)</text> </binding> </visual> </toast> • Registration. • Client apps can register with a platform specific template, e.g., • Alice’s Surface registers with Windows Store ToastText01 template. • Bob’s iPhone with the Apple JSON template:{ aps: {alert: “$(message)”}}. • Send notification. • App back-end sends a platform independent message: {message: “Hello!”}. • Version independence. • Templates can be used to abstract different client app versions. Hello! Service Bus Notification Hub • { message: “Hello!” } App back-end Hello! • { • aps: { • alert: “$(message)” • } • }
Using templates for personalization <toast> <visual> <binding template=\"ToastText01\"> <text id=\"1\">$(tempF)</text> </binding> </visual> </toast> • Registration. • Client apps can register with personalized templates, e.g., • Alice’s Surface wants to receive weather information in F degrees. • Bob’s iPhone wants weather information in C degrees. • Send notification. • App back-end sends a message including both temperatures: {tempC: “23”, tempF: “73”}. • Template Expressions. • Template support a simple expression language: • E.g., {‘Elio, ’+$(friend)+’ added you to ’+$(groupName)+‘ group’}. 73 Service Bus Notification Hub • {tempC: “23”, tempF: “73”} App back-end 23 • { • aps: { • alert: “$(tempC)” • } • }