Skip to content

Latest commit

 

History

History
212 lines (167 loc) · 12.6 KB

notification-hubs-nodejs-push-notification-tutorial.md

File metadata and controls

212 lines (167 loc) · 12.6 KB
title description keywords services documentationcenter author manager editor ms.assetid ms.service ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Sending push notifications with Azure Notification Hubs and Node.js
Learn how to use Notification Hubs to send push notifications from a Node.js application.
push notification,push notifications,node.js push,ios push
notification-hubs
nodejs
ysxu
erikre
ded4749c-6c39-4ff8-b2cf-1927b3e92f93
notification-hubs
mobile
na
javascript
article
10/25/2016
yuaxu

Sending push notifications with Azure Notification Hubs and Node.js

[!INCLUDE notification-hubs-backend-how-to-selector]

Overview

Important

To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.

This guide will show you how to send push notifications with the help of Azure Notification Hubs directly from a Node.js application.

The scenarios covered include sending push notifications to applications on the following platforms:

  • Android
  • iOS
  • Windows Phone
  • Universal Windows Platform

For more information on notification hubs, see the Next Steps section.

What are Notification Hubs?

Azure Notification Hubs provide an easy-to-use, multi-platform, scalable infrastructure for sending push notifications to mobile devices. For details on the service infrastructure, see the Azure Notification Hubs page.

Create a Node.js Application

The first step in this tutorial is creating a new blank Node.js application. For instructions on creating a Node.js application, see Create and deploy a Node.js application to Azure Web Site, Node.js Cloud Service using Windows PowerShell, or Web Site with WebMatrix.

Configure Your Application to Use Notification Hubs

To use Azure Notification Hubs, you need to download and use the Node.js azure package, which includes a built-in set of helper libraries that communicate with the push notification REST services.

Use Node Package Manager (NPM) to obtain the package

  1. Use a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Linux) and navigate to the folder where you created your blank application.
  2. Type npm install azure-sb in the command window.
  3. You can manually run the ls or dir command to verify that a node_modules folder was created. Inside that folder, find the azure package, which contains the libraries you need to access the Notification Hub.

Note

You can learn more about installing NPM on the official NPM blog.

Import the module

Using a text editor, add the following to the top of the server.js file of the application:

var azure = require('azure');

Setup an Azure Notification Hub connection

The NotificationHubService object lets you work with notification hubs. The following code creates a NotificationHubService object for the nofication hub named hubname. Add it near the top of the server.js file, after the statement to import the azure module:

var notificationHubService = azure.createNotificationHubService('hubname','connectionstring');

The connection connectionstring value can be obtained from the Azure Portal by performing the following steps:

  1. In the left navigation pane, click Browse.
  2. Select Notification Hubs, and then find the hub you wish to use for the sample. You can refer to the Windows Store Getting Started tutorial if you need help creating a new Notification Hub.
  3. Select Settings.
  4. Click on Access Policies. You will see both shared and full access connection strings.

Azure Portal - Notification Hubs

Note

You can also retrieve the connection string using the Get-AzureSbNamespace cmdlet provided by Azure PowerShell or the azure sb namespace show command with the Azure Command-Line Interface (Azure CLI).

General architecture

The NotificationHubService object exposes the following object instances for sending push notifications to specific devices and applications:

  • Android - use the GcmService object, which is available at notificationHubService.gcm
  • iOS - use the ApnsService object, which is accessible at notificationHubService.apns
  • Windows Phone - use the MpnsService object, which is available at notificationHubService.mpns
  • Universal Windows Platform - use the WnsService object, which is available at notificationHubService.wns

How to: Send push notifications to Android applications

The GcmService object provides a send method that can be used to send push notifications to Android applications. The send method accepts the following parameters:

  • Tags - the tag identifier. If no tag is provided, the notification will be sent to all clients.
  • Payload - the message's JSON or raw string payload.
  • Callback - the callback function.

For more information on the payload format, see the Payload section of the Implementing GCM Server document.

The following code uses the GcmService instance exposed by the NotificationHubService to send a push notification to all registered clients.

var payload = {
  data: {
    message: 'Hello!'
  }
};
notificationHubService.gcm.send(null, payload, function(error){
  if(!error){
    //notification sent
  }
});

How to: Send push notifications to iOS applications

Same as with Android applications described above, the ApnsService object provides a send method that can be used to send push notifications to iOS applications. The send method accepts the following parameters:

  • Tags - the tag identifier. If no tag is provided, the notification will be sent to all clients.
  • Payload - the message's JSON or string payload.
  • Callback - the callback function.

For more information the payload format, see The Notification Payload section of the Local and Push Notification Programming Guide document.

The following code uses the ApnsService instance exposed by the NotificationHubService to send an alert message to all clients:

var payload={
    alert: 'Hello!'
  };
notificationHubService.apns.send(null, payload, function(error){
  if(!error){
     // notification sent
  }
});

How to: Send push notifications to Windows Phone applications

The MpnsService object provides a send method that can be used to send push notifications to Windows Phone applications. The send method accepts the following parameters:

  • Tags - the tag identifier. If no tag is provided, the notification will be sent to all clients.
  • Payload - the message's XML payload.
  • TargetName - toast for toast notifications. token for tile notifications.
  • NotificationClass - The priority of the notification. See the HTTP Header Elements section of the Push notifications from a server document for valid values.
  • Options - optional request headers.
  • Callback - the callback function.

For a list of valid TargetName, NotificationClass and header options, check out the Push notifications from a server page.

The following sample code uses the MpnsService instance exposed by the NotificationHubService to send a toast push notification:

var payload = '<?xml version="1.0" encoding="utf-8"?><wp:Notification xmlns:wp="WPNotification"><wp:Toast><wp:Text1>string</wp:Text1><wp:Text2>string</wp:Text2></wp:Toast></wp:Notification>';
notificationHubService.mpns.send(null, payload, 'toast', 22, function(error){
  if(!error){
    //notification sent
  }
});

How to: Send push notifications to Universal Windows Platform (UWP) applications

The WnsService object provides a send method that can be used to send push notifications to Universal Windows Platform applications. The send method accepts the following parameters:

  • Tags - the tag identifier. If no tag is provided, the notification will be sent to all registered clients.
  • Payload - the XML message payload.
  • Type - the notification type.
  • Options - optional request headers.
  • Callback - the callback function.

For a list of valid types and request headers, see Push notification service request and response headers.

The following code uses the WnsService instance exposed by the NotificationHubService to send a toast push notification to a UWP app:

var payload = '<toast><visual><binding template="ToastText01"><text id="1">Hello!</text></binding></visual></toast>';
notificationHubService.wns.send(null, payload , 'wns/toast', function(error){
  if(!error){
     // notification sent
  }
});

Next Steps

The sample snippets above allow you to easily build service infrastructure to deliver push notifications to a wide variety of devices. Now that you've learned the basics of using Notification Hubs with node.js, follow these links to learn more about how you can extend these capabilities further.