Google DevFest MN - Windows Azure Notification Hubs

41
Delivering push notifications to millions of mobile devices

Transcript of Google DevFest MN - Windows Azure Notification Hubs

Page 1: Google DevFest MN - Windows Azure Notification Hubs

Delivering push notifications to millions of mobile devices

Page 2: Google DevFest MN - Windows Azure Notification Hubs

[email protected]@codel8r

thinkfirstcodelater.com

Me

adam grocholski

Page 3: Google DevFest MN - Windows Azure Notification Hubs

Wait…

Page 4: Google DevFest MN - Windows Azure Notification Hubs

…you’re from Microsoft?

Page 5: Google DevFest MN - Windows Azure Notification Hubs

Are you sure you’re in the right place?

Page 6: Google DevFest MN - Windows Azure Notification Hubs

Microsoft Google Apple

Page 7: Google DevFest MN - Windows Azure Notification Hubs

Notification Hubs…

Page 8: Google DevFest MN - Windows Azure Notification Hubs

…makes it easy to push multi-platform, personalized notifications both to single users and very large groups…

Page 9: Google DevFest MN - Windows Azure Notification Hubs

…regardless of platform.

Page 10: Google DevFest MN - Windows Azure Notification Hubs

Agenda

Push notifications 101.Why Notification Hubs.Using tags.Securing tag registrations.Using templates.Other information.

Page 11: Google DevFest MN - Windows Azure Notification Hubs

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.

Page 12: Google DevFest MN - Windows Azure 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.

Page 13: Google DevFest MN - Windows Azure Notification Hubs

Push notification lifecycle

Registration at app launch.1. Client app contacts Platform Notification Service,

to retrieve current channel (e.g., ChannelURIs, device tokens, registrationIds).

2. App updates handle in back-end.

Sending Notification.3. App back-end send notification to PNS.4. PNS pushes the notification to the app

on the device.

Maintenance.5. Delete expired handles when PNS rejects them.

PlatformNotification

Service

App back-end

Client app

Page 14: Google DevFest MN - Windows Azure Notification Hubs

Challenges of push notifications

Platform dependencyDifferent communication protocols to PNS’ (e.g., HTTP vs. TCP, xml payload vs. JSON payload).Different presentation formats and capabilities (tiles vs. toasts vs. badges).

RoutingPNS’ 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.

ScaleApp 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).

Page 15: Google DevFest MN - Windows Azure Notification Hubs

Using Notification Hubs

One-time set upCreate a Notification Hub in Service Bus.

RegisterThe 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 NotificationThe app back-end sends a message to the Notification Hub.Notification Hub pushes it to the PNS’.

GCM WNS

Notification Hub

App back-end

Android app Windows Storeapp

Page 16: Google DevFest MN - Windows Azure Notification Hubs

Advantages of using Notification HubsNo 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.

BroadcastPush notifications to millions of devices (across platforms) with a single call.

Page 17: Google DevFest MN - Windows Azure Notification Hubs

Demo

Getting started with Notification Hubs

Page 18: Google DevFest MN - Windows Azure Notification Hubs

//instantiate a new NotificationHub instance

NotificationHub hub = new NotificationHub("<hub name>", "<connection string>", context);

Register an Android app

//get the gcm id

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);

String gcmid = gcm.register(SENDER_ID);

//register with the NotificationHub

NativeRegistration r = hub.register(gcmid);

Page 19: Google DevFest MN - Windows Azure Notification Hubs

Broadcast Android notifications (C#)

//instantiate a new instance of NotificationHubClient

var hubClient = NotificationHubClient.CreateClientFromConnectionString("<connection string>", "<hub name>");

//create the json payload for the notification

var payload = "{ \"data\" : {\"msg\":\"Hello from Windows Azure!\"}}";

//send notification via gcm

hubClient.SendGcmNativeNotificationAsync(payload);

Page 20: Google DevFest MN - Windows Azure Notification Hubs

Take-aways

No need to store and maintain gcmId.In your device local storage, in the cloud.

Device registrations expire.No need to clean-up when app is uninstalled.Call register regularly.

Page 21: Google DevFest MN - Windows Azure Notification Hubs

Agenda

Push notifications 101.Why Notification Hubs.Using tags.Securing tag registrations.Using templates.Other information.

Page 22: Google DevFest MN - Windows Azure Notification Hubs

Sending notifications to specific devicesTags as interest groups.1. Client app can register with a set of tags.2. Tags are simple strings (no pre-provisioning is required).3. 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.

Notification Hub

App back-end

Tag:”Beatles”Tag:”Wailers”

Tag:”Beatles”

Page 23: Google DevFest MN - Windows Azure Notification Hubs

//instantiate a new NotificationHub instance

NotificationHub hub = new NotificationHub("<hub name>", "<connection string>", context);

Register an Android app with tags

//get the gcm id

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);

String gcmid = gcm.register(SENDER_ID);

//register with the NotificationHub using tags

NativeRegistration r = hub.register(gcmid, "Beatles");

Page 24: Google DevFest MN - Windows Azure Notification Hubs

Broadcast tagged Android notifications (C#)//instantiate a new instance of NotificationHubClient

var hubClient = NotificationHubClient.CreateClientFromConnectionString("<connection string>", "<hub name>");

//create the json payload for the notification

var payload = "{ \"data\" : {\"msg\":\"Hello from Windows Azure!\"}}";

//create a list of tags

var tags = new List<string>(){ "Beatles" };

//send notification to devices registered with tags via gcm

hubClient.SendGcmNativeNotificationAsync(payload, tags);

Page 25: Google DevFest MN - Windows Azure Notification Hubs

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.”

Page 26: Google DevFest MN - Windows Azure Notification Hubs

Agenda

Push notifications 101.Why Notification Hubs.Using tags.Securing tag registrations.Using templates.Other information.

Page 27: Google DevFest MN - Windows Azure Notification Hubs

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.1. Device does *not* contain the notification hub credentials.2. Devices authenticate with the app back-end to register.3. 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 HubApp back-

end

Page 28: Google DevFest MN - Windows Azure Notification Hubs

Take-aways

When security is needed register from the back-endNo Notification Hub SDK required on the devices

Page 29: Google DevFest MN - Windows Azure Notification Hubs

Agenda

Push notifications 101.Why Notification Hubs.Using tags.Securing tag registrations.Using templates.Other information.

Page 30: Google DevFest MN - Windows Azure Notification Hubs

Using templates for multi-platform pushRegistration.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.

Service Bus Notification HubApp back-

end

<toast><visual><binding

template=\"ToastText01\"><text

id=\"1\">$(message)</text></binding>

</visual></toast>

{aps: {

alert: “$(message)”

}}

{ message: “Hello!” }

Hello!

Hello

!

Page 31: Google DevFest MN - Windows Azure Notification Hubs

Using templates for personalization

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’}.

Service Bus Notification HubApp back-

end

<toast><visual><binding

template=\"ToastText01\"><text id=\"1\">$(tempF)</text>

</binding></visual>

</toast>

{aps: {

alert: “$(tempC)”

}}

{tempC: “23”, tempF: “73”}

73

23

Page 32: Google DevFest MN - Windows Azure Notification Hubs

//instantiate a new NotificationHub instance

NotificationHub hub = new NotificationHub("<hub name>", "<connection string>", context);

Register an Android app with templates//get the gcm id

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);

String gcmid = gcm.register(SENDER_ID);

//create the template

String template = "{ \"data\" : {\"msg\":\"$(message)"}}";

//register with the NotificationHub using the template

NativeRegistration r = hub.register(gcmid, "Template1", template, "Beatles");

Page 33: Google DevFest MN - Windows Azure Notification Hubs

Broadcast template notifications (C#)//instantiate a new instance of NotificationHubClient

var hubClient = NotificationHubClient.CreateClientFromConnectionString("<connection string>", "<hub name>");

//create expressions to substitute in the template

var expressions = var Dictionary<string, string>();

expressions.Add("message", "hello");

//send template notification to all devices

hubClient.SendTemplateNotificationAsync(expressions);

Page 34: Google DevFest MN - Windows Azure Notification Hubs

Templates take-aways

Platform agnostic code in the back-end.Abstract client app version differences from the back-end.Powerful personalization with no back-end complexity.E.g., localized messages, C/F temperatures.

Page 35: Google DevFest MN - Windows Azure Notification Hubs

Agenda

Push notifications 101.Why Notification Hubs.Using tags.Securing tag registrations.Using templates.Other information.

Page 36: Google DevFest MN - Windows Azure Notification Hubs

Delivery guarantee and telemetry

Notification Hubs do not provide delivery guarantee.All platform notification systems are “best effort”, e.g., device could be disconnected, could never reconnect, etc.Guideline: important communications have to be delivered in-app.

Telemetry.Notification Hubs provide powerful telemetry to track each and every outcome of the notifications for each platform (e.g., successful notifications, throttled notifications, expired channels/tokens).Accessible through Windows Azure portal and programmatically.

Page 37: Google DevFest MN - Windows Azure Notification Hubs

Scale

Notification Hubs run on a fully parallelized architecture.All our VMs are ready for your broadcast!A single Notification Hub scales up to millions of devices, with no special coding required.

Number of devices and latency.Notification Hubs support millions of devices out of the box.The expected latency of a broadcast to all registered devices is a couple of minutes, even for millions of devices.

Page 38: Google DevFest MN - Windows Azure Notification Hubs

Platform support

Platform support.We support push notifications through WNS, APNs, GCM, and MPNS.Device SDKs for Windows Store Apps, Windows Phone 8, iOS (Objective-C), Android.Server SDKs for .NET, Node, Mobile Services.All functions available from REST.

Page 39: Google DevFest MN - Windows Azure Notification Hubs

What Next?

Try it out. For Free.Build what you want. Scale as you need. Full access with no strings attached.http://aka.ms/thecloud

Hello startups!You have an ideas so brilliant it burns. BizSpark can help make it real.http://aka.ms/JoinBizSpark or CONTACT ME!

Page 40: Google DevFest MN - Windows Azure Notification Hubs

[email protected]@codel8r

thinkfirstcodelater.com

Contact Me

adam grocholski

Page 41: Google DevFest MN - Windows Azure Notification Hubs

© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.