Skip to content

Sample to help developers to implement Push Notification, through Azure Notification Hubs, in mobile applications.

License

Notifications You must be signed in to change notification settings

saramgsilva/NotificationHubs

Repository files navigation

Azure Notification Hubs Sample

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

Azure Notification Hubs: Best Pratices Managing Devices

◻️ Table of contents

◻️ 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:

  • The two main ways to manage devices in Azure Notification Hubs, using the Registration Model:

    • Case 1 - Devices register directly in Azure Notification Hubs
    • Case 2 - Devices register in Azure Notification Hubs through backend
  • Azure Mobile Services;

◻️ Sample Status

  • Key: ✅ = Supported, ❌ = Not Supported, 🔧 = In development
Windows Applications - Using XAML / CSharp
Platform Azure Mobile Services Registration Model (Case1) Registration Model (Case2)
Windows Store 8.1 (WinRT)
Windows Phone 8.1 (WinRT)
Windows Phone 8.1 (SL)
Windows Applications - Using XAML / Visual Basic
Platform Azure Mobile Services Registration Model (Case1) Registration Model (Case2)
Windows Store 8.1 (WinRT)
Windows Phone 8.1 (WinRT)
Windows Phone 8.1 (SL)
Xamarin Applications
Platform Azure Mobile Services Registration Model (Case1) Registration Model (Case2)
Xamarin Android
Xamarin IOS

Others

Platform Azure Mobile Services Registration Model (Case1) Registration Model (Case2)
Android Native
IOS Native
Cordova

⚠️ 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.

◻️ Requirements

  • Create the Azure Notification Hubs in Azure Portal (Azure Mobile Service create it by default)
  • Create the required data, in Push Notification Service (WNS, GCM, APNs...), to support push notification for each Platform
  • Configure the Azure Notification Hub, in Azure Portal, with the data from Push Notification Services (WNS, GCM, APNs...)
  • Add connection string and hub name from the Azure Notification Hub created (in the sample it is defined in Contants.cs file

⚠️ Change required before run each sample

The Contants.cs file should be defined, which:

  • SenderID: define the ProjectID used in Xamarin Android and Android projects to request a registrationId from Google Cloud Messaging (GCM)

  • HubName: define the Notification Hub's name created in Azure Portal

  • ConnectionString: define the Notification Hub's connection string for the client applications connect with Notification Hub.

  • BackEndConnectionString: define the Notification Hub's connection string for the backend connect with Notification Hub.

  • AMSEndpoint: define the url from the Azure Mobile Services, used by the client application when create the object to connect with Azure Mobile Services

  • AMSKey: define the admin key used by the client application when create the object to connect with Azure Mobile Services.

◻️ The Source Code

The sample has the following project's struture:

  • Shared
  • BackEnd
  • Windows
    • CSharp
      • Windows Phone 8.1 (WinRT)
      • Windows Store Apps (WinRT)
      • Windows Phone 8.1 (SL)
    • Visual Basic
      • Windows Phone 8.1 (WinRT)
      • Windows Store Apps (WinRT)
      • Windows Phone 8.1 (SL)
  • Xamarin
    • Xamarin.Android
    • Xamarin.IOS

◻️ 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.

◻️ Resources

◻️ Common Issues

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

In General

  • The connection string from Notification Hubs is wrong;
  • The xml/json that define the template are not well defined;
  • When debug the notification is not using the correct tag or forget to use the tag;
  • Is missing the configurations in Azure Portal for each application;
  • In Azure Mobile Services developers uses the Notification Hubs API to manage devices and it is not necessary because Azure Mobile Service give us it out-of-box;
  • When developers implement the registration in devices do not use the debug feature to verify if the devices was registered correctly;
  • Developers implements the Case 1 when they want to implement the Case 2 and mixes the two cases;

In Windows (WinRT) apps:

  • In manifest should be defined the Toast capable;
  • In manifest should be defined the Internet capability;
  • Associate with store;

In Windows Phone (SL) apps:

  • In manifest should be defined Internet capability, Toast capable and the ID_CAP_PushNotification
  • Should be handled the notification when the app is running

In Android apps:

  • The Project Id is wrong;
  • The GCM component is missing;
  • In debug mode, after stop the debug be aware that is need to run again the app to receive the notifications;
  • The manifest file must have the package's name starting with lower case;
  • The key used in the payload is not the same in the application;

In iOS apps:

  • To enable push notification is required the certificates that enable it. The process is not simple to create it and it is easy to do a mess between steps;
  • Developers should confirm which version of the certificate is on Azure Portal and it is used by application. This means if in Azure Portal has the production certificate the application must have the production certificate;
  • The plist must have the push notification checked and the bundle must match with the bounded created;

◻️ Tips

Azure Notification Hubs API

⚠️ It was published a new nuget: Microsoft Azure Notification Hubs

Used in Case 2 sample

Platform Azure 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 Android Azure Messaging
Xamarin IOS Azure Messaging

Used in Azure Mobile Service sample

Windows Azure Mobile Services

Push Notification Service by Platform

Platform Push 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)
Android Google Cloud Service (GCM)
IOS Apple 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

◻️ Contributors

◻️ Contribute

Everbody is welcome to contribute.

Twitter hashtag : #notificationhubs

◻️ License

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

About

Sample to help developers to implement Push Notification, through Azure Notification Hubs, in mobile applications.

http://saramgsilva.github.io/NotificationHubs/

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published