Azure / azure-notificationhubs-android

Android SDK and Samples for Azure Notification Hubs
Apache License 2.0
33 stars 62 forks source link

registerTemplate with tag is throwing exception #305

Closed mrzslr closed 2 months ago

mrzslr commented 3 months ago

Describe the bug We're trying to migrate from legacy FCM to FCM v1. I'm trying to do registerTemplate after creating NotificationHub, but it's failing to register

Exception or Stack Trace

Failed to complete registration
                                                                                                    com.microsoft.windowsazure.messaging.NotificationHubException
                                                                                                        at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:254)
                                                                                                        at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:170)
                                                                                                        at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:130)
                                                                                                        at com.microsoft.windowsazure.messaging.NotificationHub.upsertRegistrationInternal(NotificationHub.java:465)
                                                                                                        at com.microsoft.windowsazure.messaging.NotificationHub.registerInternal(NotificationHub.java:429)
                                                                                                        at com.microsoft.windowsazure.messaging.NotificationHub.registerTemplate(NotificationHub.java:201)
                                                                                                        at myapp.PushNotifications.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:72)
                                                                                                        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:78)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.os.HandlerThread.run(HandlerThread.java:67)

Code Snippet

import com.microsoft.windowsazure.messaging.NotificationHub;
import com.microsoft.windowsazure.messaging.TemplateRegistration;

NotificationHub hub = new NotificationHub(BuildConfig.AZURE_NOTIFICATION_HUB_NAME, BuildConfig.AZURE_NOTIFICATION_HUB_CONNECTION_STRING, this);

TemplateRegistration registration = hub.registerTemplate(fcmToken, notificationTemplateName, templateString, tag);

all values are provided and are not null or empty.

Expected behavior To register with the template and return TemplateRegistration.

Setup (please complete the following information):

Additional context following https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-sdk

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

lomagdal2 commented 2 months ago

Greetings!

Our template validation logic has changed in FCM V1 and templates used for FCM Legacy may not be compatible with FCMV1 Templates. To help troubleshoot your issue, can you please provide us with value for templateString?

mrzslr commented 2 months ago

Greetings!

Our template validation logic has changed in FCM V1 and templates used for FCM Legacy may not be compatible with FCMV1 Templates. To help troubleshoot your issue, can you please provide us with value for templateString?

trying to register a custom template. templateString looks something like this: "{\"data\":{\"message\":\"$(message)\"}}"

lomagdal2 commented 2 months ago

Thanks for replying.

It looks like your template is missing a message object. Try nesting your current template inside of a message object.

We've got some documentation posted here which may help - https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-sdk#android-sdk.

Pulling from the doc to save you a click-

Update the payload template. If you're not using templates, you can skip this step.

For example, if you're using registrations:

   NotificationHub hub = new NotificationHub(BuildConfig.hubName, BuildConfig.hubListenConnectionString, context);
   String template = "{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}";
   hub.registerTemplate(token, "template-name", template);

If you're using installations:

   InstallationTemplate testTemplate = new InstallationTemplate(); 
   testTemplate.setBody("{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}");  
   NotificationHub.setTemplate("testTemplate", testTemplate);
mrzslr commented 2 months ago

Thanks for replying.

It looks like your template is missing a message object. Try nesting your current template inside of a message object.

We've got some documentation posted here which may help - https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-sdk#android-sdk.

Pulling from the doc to save you a click-

Update the payload template. If you're not using templates, you can skip this step.

For example, if you're using registrations:

NotificationHub hub = new NotificationHub(BuildConfig.hubName, BuildConfig.hubListenConnectionString, context);
String template = "{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}";
hub.registerTemplate(token, "template-name", template);

If you're using installations:

InstallationTemplate testTemplate = new InstallationTemplate(); 
testTemplate.setBody("{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}");  
NotificationHub.setTemplate("testTemplate", testTemplate);

Thank you for your response @lomagdal2 I changed and updated the payload. can see it's successfully registered to ANH. but when I choose Custom Template in Send Test to send a notification with payload {"message":"Notification Hub test notification"} or with Android(FCM v1) { "message": { "notification": { "body" : "message body"} } } can't see the notification has been sent to registration id I expected in Result section.

mrzslr commented 2 months ago

Thanks for replying. It looks like your template is missing a message object. Try nesting your current template inside of a message object. We've got some documentation posted here which may help - https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-sdk#android-sdk. Pulling from the doc to save you a click- Update the payload template. If you're not using templates, you can skip this step. For example, if you're using registrations:

NotificationHub hub = new NotificationHub(BuildConfig.hubName, BuildConfig.hubListenConnectionString, context);
String template = "{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}";
hub.registerTemplate(token, "template-name", template);

If you're using installations:

InstallationTemplate testTemplate = new InstallationTemplate(); 
testTemplate.setBody("{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}");  
NotificationHub.setTemplate("testTemplate", testTemplate);

Thank you for your response @lomagdal2 I changed and updated the payload. can see it's successfully registered to ANH. but when I choose Custom Template in Send Test to send a notification with payload {"message":"Notification Hub test notification"} or with Android(FCM v1) { "message": { "notification": { "body" : "message body"} } } can't see the notification has been sent to registration id I expected in Result section.

So sorry about the previous comment. it was due to Send Test randomness. I had more devices than 10, so the expected one wasn't included. It would be good tho to have an option for Send Test to uncheck the randomness.

I have another question about verifying if the implementation is happening by the current GCM or FCM v1. For example if currently using GCM and updating the FCM v1 info, wanna make sure after migration, FCM v1 will work. Appreciate it!

lomagdal2 commented 2 months ago

Hello!

Our debug send response will contains a results property, which is an array of registration results for the debug send. Each registration result will specify the application platform. In your case, you should see FCM V1 as the application platform and not GCM / FCM Legacy.

Additionally, we offer per message telemetry for standard tier notification hubs. This telemetry features GcmOutcomeCounts and FcmV1OutcomeCounts which can help you verify which platform is being used for sends.

This issue was opened due to being unable to register an FCM V1 template registration. As you're now able to create the registrations, we'll be closing this issue. Feel free to open another ticket if you have a different issue.