Search code examples
azureazure-functionsazure-iot-hubazure-notificationhubazure-servicebus-queues

Azure - trigger notification from service bus queue


I am trying to create the following process:

An IoT device sends a message to the IoT Hub which in turn, if the message contains some value, triggers a notification to all of the registered android devices through the Notification Hub.

At first, I do not care what is the content of the message itself, and I just want to trigger a notification on every incoming message.

I have managed to set up the IoT device and connect it to the IoT hub. I have also managed to set up the Notification Hub, and connect it with the android app, in a way that when I use the "test send" in the Notification Hub, a notification is received on the android device.

In order to connect both ends (IoT Hub and Notification Hub), I tried to follow the following tutorial: https://www.developer.com/ws/android/sending-notifications-to-mobile-apps-from-azure-function-apps.html

In addition, I have added routing from the IoT Hub to the appropriate queue in the service bus.

Now, whenever the IoT device sends a message to the hub, I can see that the queue receives a message. However, I can't seem to use the queue to trigger the notification.

The ServiceHubQueueTrigger function I have added has the following error in the portal:

Error:

Function (ServiceBusQueueTrigger1) Error: The binding type(s) 'notificationHub' are not registered. Please ensure the type is correct and the binding extension is installed.

Its function.json looks like this:

{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "notificationqueue",
      "connection": "ServiceBusConnection",
      "accessRights": "manage"
    },
    {
      "name": "notification",
      "type": "notificationHub",
      "hubName": "<hub-name>",
      "connection": "NotificationConnString",
      "platform": "gcm",
      "tagExpression": "",
      "direction": "out"
    }
  ]
}

where hub-name is the name of the Notification Hub.

How can I trigger the notifications to the android devices through the queue? Is there a way to trigger them directly from an IoT Hub Event Trigger function?

Thanks!


Solution

  • It seems like a simple 1.x vs 2.x Functions version problem indeed.

    To solve it, if you are using the Azure portal:

    1. Create a new Function App.
    2. In the Function App Settings, set the version to ~1: enter image description here Note that for a new Function App, the option to set the version won't be grayed out and you could set it to ~1.
    3. Then, follow the guide at the link in the quetion.

    If you are using Visual Studio, simply choose v1 when creating the function. enter image description here

    As written in Microsoft's documentation, Notification binding do not support Functions 2.x, unfortunately.