skip to Main Content

Re-submitting this question because we’ve made quite a bit of progress, but not quite sorted yet. Been using this library and functions successfully since 2021 in my Android App

com.microsoft.azure:notification-hubs-android-sdk-fcm:1.1.4

registerTemplate creates GcmTemplateRegistrationDescription and GcmRegistrationId fine

Need to create FcmV1TemplateRegistration and FcmV1RegistrationId so updated to 2.0.0

com.microsoft.azure:notification-hubs-android-sdk:2.0.0

Call to registerTemplate on device now creates FcmV1TemplateRegistration in Azure Hub

(Get Android device regid with FirebaseMessaging.getInstance().getToken() function first)

String messageTemplate = "{"message":{"android":{"data":{"msgtext":"$(msgtext)","msgsndr":"$(msgsndr)"}}}}";
String RegistrationTagTopic = "5050";
mobile_RegistrationId = hub.registerTemplate(regid, "CAREV1", messageTemplate, RegistrationTagTopic).getRegistrationId();

Example registration: mobile_RegistrationId returned above is Notification Hub RegistrationId

<ETag>5</ETag>
<ExpirationTime>2124-06-12T05:55:24.1941413Z</ExpirationTime>
<RegistrationId>2837404...516-2</RegistrationId>
<Tags>5050</Tags>
<FcmV1RegistrationId>eZMPxd2pQBi...Uh</FcmV1RegistrationId>
<BodyTemplate><![CDATA[{"message":{"android":{"data":{"msgtext":"$(msgtext)","msgsndr":"$(msgsndr)"}}}}]]></BodyTemplate>
<TemplateName>CAREV1</TemplateName>
</FcmV1TemplateRegistrationDescription>

It seems the format of the template in the App for FCMv1 is absolutely critical because my App threw an exception on register with any other format than {"message":{"android":{"data":{…}}}}

Send to device tag 5050 using Azure Notification Hubs REST API Method, same API call we’ve used successfully since 2015 (presume you know how to generate your Authorization token): Send a template notification with parameters like;

ServiceBusNotification-Format = "template"
ServiceBusNotification-Tags = "5050"
ContentType = "application/json;charset=utf-8."
Method = "POST"
URI = "https://{namespace}.servicebus.windows.net/{NotificationHub}/messages/?api-version=2015-01"

Example of JSON posted in the call

{"message":{"android":{"data":{"msgtext":"My short message","msgsndr":"ADMIN"}}}}

Turns out this is just plain wrong at the server end (see below). Notification received on device, but no payload

Logged using &test mode with REST API, everything looked like it worked, Reg Id and PnsHandle both match and recognizes fcmV1Template but it didn’t..!

address    : 5050
Success : 1
Failure : 0
Appform : fcmV1Template
Reg  Id : 2837404...516-2
PnsHandle : eZMPxd2pQBi...Uh
Outcome : The Notification was successfully sent to the Push Notification System

Send (POST) same message direct to 5050 via FCM/JWT routine, slightly different JSON format;

{"message":{"topic":"5050","data":{"msgtext":"My short message","msgsndr":"ADMIN"},"android":{"priority":"high"},"fcm_options":{"analytics_label":"xyz"}}}

Notification and message payload are perfect (note extra FCM parameters) and works every time. Of course, FCM doesn’t require a template in the App itself or the server process.

Question: What’s going wrong with my template send via Notification Hubs..?

RESOLVED 18 JUNE 2024 17:12 NZST

Seems that I didn’t need to alter the JSON structure of the Azure Notification Hubs REST API Method at all.

Notification hubs figured out that one device App is FcmV1TemplateRegistration with fcmV1Template while the other is GcmTemplateRegistration with gcmTemplate since the App hasn’t updated yet. The JSON sent by our server only resembles the Data: branch in the JSON template registered by the Android App in Notification Hubs.

Here is my log using the original JSON structure with &test parameter on;

Post URI https://powerpage.servicebus.windows.net/care/messages/?api-version=2015-01&test

ContentType application/json;charset=utf-8.
{"msgaddr":"65552","msgsndr":"ADMIN","msgtext":"Re-Test with original server"}
call webrequest
Response WebRequest StatusCode:        201
Response WebRequest StatusDescription: Created
WebRequest succeeded with status code: 201 Description: Created
address    : 65552
Success : 1
Failure : 0
Appform : gcmTemplate
Reg  Id : 7873109321995......789764826990-2
PnsHandle : dZJC0CJ6QOea_y7Gr......jCx_TTaPfOlYlo3NIrQ9rmbOHaUg2aRoTDW0rCrFOAy4Gk1
Outcome : The Notification was successfully sent to the Push Notification System

ContentType application/json;charset=utf-8.
{"msgaddr":"5050","msgsndr":"ADMIN","msgtext":"Re-Test with original server"}
call webrequest
Response WebRequest StatusCode:        201
Response WebRequest StatusDescription: Created
WebRequest succeeded with status code: 201 Description: Created
address    : 5050
Success : 1
Failure : 0
Appform : fcmV1Template
Reg  Id : 2837404753035......116714665516-2
PnsHandle : eZMPxd2pQBic3hfuF......fNXW4qT5-UhCFM1W3ZDL1XZw7oiLaTDJk1v7tqnw-xEe2i
Outcome : The Notification was successfully sent to the Push Notification System

Enjoy

2

Answers


  1. Chosen as BEST ANSWER

    "Support of Azure Notification Hubs’ Firebase Cloud Messaging legacy API will be retired by 20 June 2024"

    If you're getting these emails, here's what you have to do in your Android App;

    1. Update sdk in build.gradle (:app)

    implementation 'com.microsoft.azure:notification-hubs-android-sdk:2.0.0'

    1. Update the template in your App for the registerTemplate call

      String messageTemplate = 
      "{"message":{"android":{"data": 
      {"yourtext":"$(yourtext)","msgsndr":"$(msgsndr)"}}}}";
      

    Be sure to stick with their overall JSON structure above, otherwise your App will crash on register.

    Inside the data: bracket is free-form, but just use the same fields from your existing template, then...

    If you are using the Azure Notification Hubs REST API Method, no change is required to your server process, assuming you didn't change anything in the JSON payload.

    The SDK registers devices as FCMv1 automatically and the Notification Hub uses the appropriate FcmV1TemplateRegistrationDescription

    Credentials should already be configured on the Hub for Google (FCM v1). Get the values from your xxx-hub-firebase-adminsdk-xxxx-xxxxxxx.json file and you're good to go...

    Hope this isn't too late for you, I only uploaded revised App to Google Play and hour ago. Good luck..!


  2. Try changing your payload to where data is outside of the android field. Any variables you want to send to the phone to process need to be inside the data field. I would also drop fcm_options. Our payload is structured like the following:

    {
     "message": {
      "android":{
       "priority":"high"
      },
      "data": {
       “message”:””,
       “title”:””,
       “datafield”:””,
      }
     }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search