Home

Awesome

Azure Notification Hubs Sample

This sample is related to the article from MSDN Magazine (April 2015):

Azure Notification Hubs: Best Pratices Managing Devices

<MTMarkdownOptions output='html4'> <a href="https://msdn.microsoft.com/pt-pt/magazine/dn948105(en-us).aspx"><img src="http://s20.postimg.org/ayjtmjbh9/MSDNMagazine_2.png" height="400"/></a>

:white_medium_square: Table of contents

:white_medium_square: What is this?

The sample provided has the goal to help developers add Push Notifications to their applications, through Azure Notification Hubs and this sample supports:

:white_medium_square: Sample Status

Windows Applications - Using XAML / CSharp
PlatformAzure Mobile ServicesRegistration Model (Case1)Registration Model (Case2)
Windows Store 8.1 (WinRT):white_check_mark::white_check_mark::white_check_mark:
Windows Phone 8.1 (WinRT):white_check_mark::white_check_mark::white_check_mark:
Windows Phone 8.1 (SL):white_check_mark::white_check_mark::white_check_mark:
Windows Applications - Using XAML / Visual Basic
PlatformAzure Mobile ServicesRegistration Model (Case1)Registration Model (Case2)
Windows Store 8.1 (WinRT):white_check_mark::white_check_mark::white_check_mark:
Windows Phone 8.1 (WinRT):white_check_mark::white_check_mark::white_check_mark:
Windows Phone 8.1 (SL):white_check_mark::white_check_mark::white_check_mark:
Xamarin Applications
PlatformAzure Mobile ServicesRegistration Model (Case1)Registration Model (Case2)
Xamarin Android:white_check_mark::white_check_mark::white_check_mark:
Xamarin IOS:white_check_mark::white_check_mark::white_check_mark:

Others

PlatformAzure Mobile ServicesRegistration Model (Case1)Registration Model (Case2)
Android Native:white_check_mark::x::x:
IOS Native:x::white_check_mark::x:
Cordova:x::x::x:

:warning: Notes:

  1. The Windows Phone 8.0 implementation is similar to Windows Phone 8.1 SL implementation, this way it will not be provided.

  2. For Cordova apps, see the article MSDN Magazine - Push Notifications to Cordova Apps with Microsoft Azure by Glenn Gailey.

:white_medium_square: Requirements

:warning: Change required before run each sample

The Contants.cs file should be defined, which:

:white_medium_square: The Source Code

The sample has the following project's struture:

:white_medium_square: Build the projects

To develop on this project, just clone the project to your computer, package restore is enable so build the solution first, if you get any errors try to build again and if necessary close the solution and open again to load the references.

:white_medium_square: Resources

:white_medium_square: Common Issues

The following list provide some common issues I found when I did the sample or even when help others developers.

In General

In Windows (WinRT) apps:

In Windows Phone (SL) apps:

In Android apps:

In iOS apps:

:white_medium_square: Tips

Azure Notification Hubs API

:warning: It was published a new nuget: Microsoft Azure Notification Hubs

Used in Case 2 sample

PlatformAzure Notification Hubs API
Windows Store 8.1 (WinRT)WindowsAzure.Messaging.Managed
Windows Phone 8.1 (WinRT)WindowsAzure.Messaging.Managed
Windows Phone 8.1 (SL)WindowsAzure.Messaging.Managed
Xamarin AndroidAzure Messaging
Xamarin IOSAzure Messaging

Used in Azure Mobile Service sample

Windows Azure Mobile Services

Push Notification Service by Platform

PlatformPush Notification Service
Windows Store 8.1 (WinRT)Windows Push Notification Services (WNS)
Windows Phone 8.1 (WinRT)Windows Push Notification Services (WNS)
Windows Phone 8.1 (SL)Microsoft Push Notification Service (MPNS)
Windows Phone 8.0 (SL)Microsoft Push Notification Service (MPNS)
AndroidGoogle Cloud Service (GCM)
IOSApple Push Notification Service (APNs)

Push Notification Templates by platform

Windows Phone 8.1 and Windows Store apps (WinRT)

var payload = new XElement("toast",
                         new XElement("visual",
                            new XElement("binding",
                                new XAttribute("template", "ToastText01"),
                                new XElement("text",
                                    new XAttribute("id", "1"), message)))).ToString(SaveOptions.DisableFormatting);

Windows Phone 8.1 SL

var mpnsPushMessage  = new MpnsPushMessage(toast);
        XNamespace wp = "WPNotification";
        XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", null),
            new XElement(wp + "Notification", new XAttribute(XNamespace.Xmlns + "wp", "WPNotification"),
                new XElement(wp + "Toast",
                    new XElement(wp + "Text1",
                         "Notification Hubs Sample"),
                    new XElement(wp + "Text2", message))));

var xmlPayload = string.Concat(doc.Declaration, doc.ToString(SaveOptions.DisableFormatting));

IOS

var alert =new JObject(
                new JProperty("aps", new JObject(new JProperty("alert", notificationText))),
                new JProperty("inAppMessage", notificationText))
                .ToString(Newtonsoft.Json.Formatting.None);

Android

var payload = new JObject(
                    new JProperty("data", new JObject(new JProperty("message", notificationText))))
                    .ToString(Newtonsoft.Json.Formatting.None);

Development In Xamarin.Android

If you are using simulator and do the deploy for install the app or debug it, you can receive Push Notification while the app run, but if you stop and try to send another Push Notification the simulator will not receive it, you should run again the app without VS to get the push notification

:white_medium_square: Contributors

<MTMarkdownOptions output='html4'> <a href="https://twitter.com/saramgsilva"><img src="https://www.gravatar.com/avatar/8432690438afd20b4de8cd6bbf062549?s=328&d=identicon&r=PG" height="50"/></a> <a href="https://twitter.com/paulomorgado"><img src="http://saramgsilva.github.io/NotificationHubs/images/PauloMorgado_320x240_400x400.jpg" height="50"/></a> <a href="https://twitter.com/adpead"><img src="http://saramgsilva.github.io/NotificationHubs/images/gn8frj8ipi0rsntcvcd0_400x400.jpeg"/ height="50"></a> <a href="https://twitter.com/clerigo"><img src="http://saramgsilva.github.io/NotificationHubs/images/EbslN-rW_400x400.jpeg"/ height="50"></a> </MTMarkdownOptions><a href="https://twitter.com/Totemika"><img src="http://s20.postimg.org/77ylj1vj1/Nz2_Jo_RA2_400x400.jpg"/ height="50"></a> </p> </MTMarkdownOptions> </p>

:white_medium_square: Contribute

Everbody is welcome to contribute.

Twitter hashtag : #notificationhubs

:white_medium_square: License

MIT License (MIT), read more about it in the LICENSE file.