How do I add an analytics label to data messages with the java Firebase Admin SDK?
W

3

5

As of July 1, Firebase requires the addition of labels to messages in order for analytics to show about the number of data message sends etc.

'Starting Monday, 1 July 2019, you will be able to filter data by an analytics label. Data messages sent without an analytics label might not be represented in this dashboard after that date.'

The problem is that the link to the documentation provided by firebase here, does not help. It only refers to HTTP requests whilst I am using the Firebase Admin SDK running on a Java app engine instance.

This is the barebones of the code I am using to currently send data messages. How can I adapt this to send my messages with an analytics label?

Message message = Message.builder()
                .putData("data",data)
                .setToken(deviceID)
                .build();


        String response = FirebaseMessaging.getInstance().send(message);
Whisker answered 7/7, 2019 at 9:23 Comment(0)
S
6

I think I found it:

Message message = Message.builder()
    .putData("data",data)
    .setFcmOptions(FcmOptions.withAnalyticsLabel("MyLabel"))
    .setToken(deviceID)
    .build();

I can see messages in Firebase console -> Reports -> Data tab.

Succinic answered 7/8, 2019 at 8:31 Comment(1)
Worked like a charm. Upgrade SDK if you don't find this method. I am using 6.10Mathewson
W
0

From looking at the commit history of the messaging section of the SDK, it seems like support for this header has not been added to the Firebase Admin SDK for Java yet.

You might want to file a feature request or (even better) open a PR. I left a comment on this existing feature request, which seemed most relevant to what you're asking.

Wheeled answered 7/7, 2019 at 13:16 Comment(0)
D
0

The support to add analytic label from Java code is done in Firebase API under this commit

So, using this I got it working. Below is the snippet, and it uses import com.google.firebase.messaging.FcmOptions;

Message.builder()
.setFcmOptions(FcmOptions.builder().setAnalyticsLabel("my_analytic_label").build())

Note: the analytic label reflects on the firebase console after a lag of 24 hours.

An interesting part was to unit test and verify the changes. As the getters for all the properties are marked with @VisibleForTesting, see Message

I referred their unit test MessageTest class and used the same logic to perform the assertions.

private static Map<String, Object> toMap(Object object) throws IOException {
        JsonFactory jsonFactory = ApiClientUtils.getDefaultJsonFactory();
        String json = jsonFactory.toString(object);
        JsonParser parser = jsonFactory.createJsonParser(json);
        Map<String, Object> map = new HashMap<>();
        parser.parse(map);
        return map;
    }

And now using this, we can pass the message object and verify the properties as required.

Map<String, Object> messageProps = toMap(message);
    assertThat(messageProps.get("fcm_options").toString()).hasToString("{analytics_label=my_analytic_label}");
Dianetics answered 5/11 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.