This document discusses using Notification Hubs to send push notifications from a backend to Android and Windows apps. It shows code for registering devices with Notification Hubs and sending notifications using GCM and WNS. It also demonstrates using templates and tags to target specific devices with customized payloads. The document promotes Microsoft Azure services and provides contact information.
1 of 41
Downloaded 14 times
More Related Content
Google DevFest MN - Windows Azure Notification Hubs
18. //get the gcm id
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String gcmid = gcm.register(SENDER_ID);
//instantiate a new NotificationHub instance
NotificationHub hub = new NotificationHub("<hub name>", "<connection
string>", context);
//register with the NotificationHub
NativeRegistration r = hub.register(gcmid);
19. //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);
20. No need to store and maintain gcmId.
Device registrations expire.
23. //get the gcm id
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String gcmid = gcm.register(SENDER_ID);
//instantiate a new NotificationHub instance
NotificationHub hub = new NotificationHub("<hub name>", "<connection
string>", context);
//register with the NotificationHub using tags
NativeRegistration r = hub.register(gcmid, "Beatles");
24. //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);
32. //get the gcm id
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String gcmid = gcm.register(SENDER_ID);
//instantiate a new NotificationHub instance
NotificationHub hub = new NotificationHub("<hub name>", "<connection
string>", context);
//create the template
String template = "{ "data" : {"msg":"$(message)"}}";
//register with the NotificationHub using the template
NativeRegistration r = hub.register(gcmid, "Template1", template, "Beatles");
33. //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);
39. 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!