Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register devices to Azure Notification Hub from server side(with NodeJS sdk) ?

I am developing a windows phone 8.1 App (RT), I am trying to push notification with Azure Notification Hub. I am able to do it with the client side SDK available. But I want to do the device registration, tagging etc. from the server side. I see a good guide for .Net backend at http://blogs.msdn.com/b/azuremobile/archive/2014/04/08/push-notifications-using-notification-hub-and-net-backend.aspx . I am using NodeJS in the backend server side. Can anyone help me in the same, with a sample code or so.

  • I want to register devices from server side (iPhone, Android & Windows Phone), actually I have the device tokens available at the servicer side which is sent from the device via API call.
  • I want to update multiple tags for each devices time to time.
  • I want to unregister the devices when user request to do so.
  • I want to send push notification to specific tags, using the template.
like image 464
Ameen Rashad Avatar asked Nov 20 '14 06:11

Ameen Rashad


People also ask

What is Azure Notification Hub?

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. The first step in this tutorial is creating a new blank Node.js application.

How do I register devices with a Notification Hub?

Device registration with a Notification Hub is accomplished using a Registration or Installation. A registration associates the Platform Notification Service (PNS) handle for a device with tags and possibly a template. The PNS handle could be a ChannelURI, device token, or FCM registration ID.

How many tags does Azure Notification Hub support?

Azure Notification Hubs supports a maximum of 60 tags per device. An Installation is an enhanced registration that includes a bag of push related properties. It is the latest and best approach to registering your devices using the server-side .NET SDK ( Notification Hub SDK for backend operations ).

How does the PNS handle register with the Notification Hub?

The device first retrieves the PNS handle from the PNS, then registers with the notification hub directly. After the registration is successful, the app backend can send a notification targeting that registration. For more information about how to send notifications, see Routing and Tag Expressions.


1 Answers

The steps to register the device token and sending out the notification using the Notification Hub in node.js are as follows:

  1. Create a Registration ID
  2. Create Registration
  3. Send Notification

This is server side code, once the device token is received. Note that the registration ID, Device Token, Tag and Callback function are required parameters for notificationHubService.apns.send call.

Here is the code snippet:

var azure = require('azure');

var notificationHubService = azure.createNotificationHubService('<Hub Name>','<Connection String>');
var payload={
        alert: 'Hello!'
      };

notificationHubService.createRegistrationId(function(error, registrationId, response){

      if(!error){
        console.log(response);
        console.log(registrationId);


        //RegistrationDescription registration = null;
        //registration.RegistrationId = registrationId;
        //registration.DeviceToken = req.body.token;
        notificationHubService.apns.createOrUpdateNativeRegistration(registrationId, req.body.token, req.token.upn, function(error, response){

            if(!error){
              console.log('Inside : createOrUpdateNativeRegistration' + response);

                notificationHubService.apns.send(null, payload, function(error){
                if(!error){
                  // notification sent

                  console.log('Success: Inside the notification send call to Hub.');

                }
              });

            }
            else{
              console.log('Error in registering the device with Hub' + error);
            }

        });

      }
      else{
        console.log('Error in generating the registration Id' + error);
      }

  });
like image 130
EmbCoder Avatar answered Oct 04 '22 21:10

EmbCoder