This article is contributed. See the original author and article here.

This article will introduce how to use Azure Notification Hub REST API to send a notification to device with Baidu Push. It provides an example of how to retrieve the PNS handle and send notification to device using REST API with Baidu Push.


yawhu_0-1614060573245.png


 


Pre-requirement :


Before starting, you must setup a Baidu Push environment and create a application in Android Studio.



 


Getting started with Azure Notification Hub using Baidu



  1. Enter API Key and SECRET KEY in Azure Portal -> Notification Hub -> Baidu (Android China).
    yawhu_1-1614060573255.png
    Note : Go to the Configuration page of the Baidu Push application and you can find API KEY and SECRET KEY.
    yawhu_2-1614060573262.png

  2. Execute your application in Android Studio and you should see below keywords inyawhu_3-1614060573263.png.
    errorCode=0 : 0 means the device is registered successfully via Baidu SDK.
    userId : copy this for next step
    channelId: copy this for next step
    yawhu_4-1614060573312.png

  3. Create a registration for the device. This method generates a registration ID, which you can subsequently use to retrieve, update, and delete this registration.
    – Request













    Method



    Request URI



    POST



    https://<notification_hub_namespace>.servicebus.windows.net/<notification_hub_name>/registrations/?api-version=2015-01 




    – Request Headers






















    Request Header



    Description



    Content-Type



    application/atom+xml;type=entry;charset=utf-8



    Authorization



    Azure Service Bus access control with Shared Access Signatures – Azure Service Bus | Microsoft Docs



    x-ms-version



    2015-01




    – Request Body
    XML

    <?xml version="1.0" encoding="utf-8"?>
    <entry xmlns=http://www.w3.org/2005/Atom>
        <content type="application/xml">
            <BaiduRegistrationDescription xmlns:i=http://www.w3.org/2001/XMLSchema-instance xmlns=http://schemas.microsoft.com/netservices/2010/10/servicebus/connect>
                <BaiduUserId>{userId}</BaiduUserId>
                <BaiduChannelId>{channelId}</BaiduChannelId>
            </BaiduRegistrationDescription >
        </content>
    </entry>


    – Response Code













    Code



    Description



    200



    Registration created successfully.




    – Response Body
    Upon success, a validated Atom entry is returned. It includes read-only elements such as ETag, RegistrationId, and ExpirationTime. For example:
    XML

    <entry a:etag="W/&quot;1&quot;" xmlns=http://www.w3.org/2005/Atom xmlns:a=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata>
        <id>https://notificationhubn.servicebus.windows.net/notificationhub1/registrations/{registerId}?api-version=2015-01</id>
        <title type="text">{registerId}</title>
        <published>2021-02-19T06:52:22Z</published>
        <updated>2021-02-19T06:52:22Z</updated>
        <link rel="self" href=https://notificationhubn.servicebus.windows.net/notificationhub1/registrations/{registerId}?api-version=2015-01/>
        <content type="application/xml">
            <BaiduRegistrationDescription xmlns=http://schemas.microsoft.com/netservices/2010/10/servicebus/connect xmlns:i=http://www.w3.org/2001/XMLSchema-instance>
                <ETag>1</ETag>
                <ExpirationTime>9999-12-31T23:59:59.999</ExpirationTime>
                <RegistrationId>{registerId}</RegistrationId>
                <BaiduUserId>{userId}</BaiduUserId>
                <BaiduChannelId>{channelId}</BaiduChannelId>
            </BaiduRegistrationDescription>
        </content>
    </entry>​


  4. Use Test Send to check whether the device is registered successfully in the previous step.
    yawhu_5-1614060573322.png

  5. Sends a Baidu native notification through a notification hub.
    – Request













    Method



    Request URI



    POST



    https://<notification_hub_namespace>.servicebus.windows.net/<notification_hub_name>/messages/?api-version=2015-04 




    – Request Headers





























    Request Header



    Description



    Content-Type



    application/json;charset=utf-8



    Authorization



    Azure Service Bus access control with Shared Access Signatures – Azure Service Bus | Microsoft Docs



    ServiceBusNotification-DeviceHandle



    {RegistrationId}



    ServiceBusNotification-Format



    baidu



    x-ms-version



    2015-04




    – Request Body
    JSON

    {"title":"Title","description":"Notification Hub test notification"}​


    – Response Code














    Code



    Description



    201



    Message successfully send to Baidu.




  6. Your device should receive the notification you send in previous step in few seconds.


 


Additional Reference :


Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.