0
votes

I am currently using Azure notification hub(FCM) to send one-one notification to user as well as notification to group of users by using tags(5000 - 10000 users at a time) .

Now while sending notification to group , I want some personalization like:
Hi ABC<$(firstname of user1)>, here is new AAAAA for you today.
Hi XYZ<$(firstname of user2)>, here is new AAAAA for you today.
.
.
Hi ZZZ<$(firstname of user5000)>, here is new AAAAA for you today.

I read that this is possible by using push variables with native registartion /installation sdk. Ref:https://azure.microsoft.com/en-in/blog/updates-from-notification-hubs-independent-nuget-installation-model-pmt-and-more/
But I could not find any option in registration/installation Java SDK to set these values .

Registration registration = new FcmRegistration(id, token);
registration.getTags().add(tagname);
hub.createRegistration(registration);

Installation installation = new Installation(name);
installation.setPushChannel(token);
installation.setPlatform(NotificationPlatform.Gcm);
installation.addTag(tagname);
hub.createOrUpdateInstallation(installation);

Any help is really appreciated , otherwise for group notification I have to send notification for each user via iteration and that defeats benefit of using tags and getting the job done in just 1 hub API call.

3

3 Answers

0
votes

You are correct - this is exactly what ANH templates are for. You can read this blog post about them for some background knowledge. Essentially, once you've created a template you can do a template send operation that provides just the values that need to be injected. i.e. Your Installation record will have set the appropriate body:

"Hi $(firstname), here is new $(subject) for you today."

and your send operation provides the values to inject:

{
    "firstname": "ABC",
    "subject": "AAAAA"
}

Also, make sure to specify the correct tags to scope the audience, in this case something like "ABC" to specify the user, and "new-daily" to specify which templates should be used.

Another trick, you can skip a layer of tag management and send fewer requests by embedding the $(firstname) in the template itself.

"Hi ABC, here is new $(subject) for you today."

Because templates are stored per device, each device can have a separate name embedded in it, reducing the number of tags you need to tinker with. This would make the body you send just:

{
    "subject": "AAAAA"
}

and you only need to scope with the tag "new-daily".

0
votes

Looks like you're on the right track with templating. When you embed an expression into surrounding text, you're effectively doing concatenation, which requires the expression to be surrounded in { }. See documentation about template expression language using Azure Notification Hubs where it states "when using concatenation, expressions must be wrapped in curly brackets."

In your case, I think you want something along the lines of:

... 
{"title":"{'Seattle Kraken vs. ' + $(opponent) + ' starting soon'}",...}
...
0
votes

Thanks a lot I got it working by extending the API classes on my own in following manner as per the blog suggested.

package com.springbootazure.controller;

import java.util.Map;

import org.apache.commons.collections.map.HashedMap;

import com.google.gson.GsonBuilder;
import com.windowsazure.messaging.FcmRegistration;

public class PushRegistration extends FcmRegistration {
    private static final String FCM_NATIVE_REGISTRATION1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"><content type=\"application/xml\"><GcmRegistrationDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">";
    private static final String FCM_NATIVE_REGISTRATION2 = "<GcmRegistrationId>";
    private static final String FCM_NATIVE_REGISTRATION3 = "</GcmRegistrationId></GcmRegistrationDescription></content></entry>";

    private Map<String, String> pushVariables = new HashedMap();

    public PushRegistration() {
        super();
    }

    public PushRegistration(String registrationId,
            String fcmRegistrationId, Map<String, String> pushVariables) {
        super(registrationId, fcmRegistrationId);
        this.pushVariables = pushVariables;
    }

    public PushRegistration(String fcmRegistrationId, Map<String, String> pushVariables) {
        super(fcmRegistrationId);
        this.pushVariables = pushVariables;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result
                + ((pushVariables == null) ? 0 : pushVariables.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        PushRegistration other = (PushRegistration) obj;
        if (pushVariables == null) {
            if (other.pushVariables != null) {
                return false;
            }
        } else if (!pushVariables.equals(other.pushVariables)) {
            return false;
        }
        return true;
    }

    protected String getPushVariablesXml() {
        StringBuilder buf = new StringBuilder();
        if (!tags.isEmpty()) {
            buf.append("<PushVariables>");

            buf.append(new GsonBuilder().disableHtmlEscaping().create().toJson(pushVariables));

            buf.append("</PushVariables>");
        }
        return buf.toString();
    }

    @Override
    public String getXml() {
        return FCM_NATIVE_REGISTRATION1 +
                getTagsXml() +
                getPushVariablesXml() +
                FCM_NATIVE_REGISTRATION2 +
                fcmRegistrationId +
                FCM_NATIVE_REGISTRATION3;
    }
}

And afterwards , register a token using :

    Map<String, String> pushVariables = new HashMap<>();
    pushVariables.put("firstname", "Gaurav");
    pushVariables.put("lastname", "Aggarwal");
    Registration registration = new PushRegistration(name, token, pushVariables);
    if (registration == null) {
        registration = new FcmRegistration(name, token);
    }

    registration.getTags().add(tagname);
    registration.getTags().add(category);
    hub.createRegistration(registration);

And then send notification like:

Notification n = Notification.createFcmNotifiation("{\n" +
                " \"notification\" : {\n" +
                "  \"body\" :  \"{ $(firstname) + ' starting soon'}\",\n" +
                "  \"title\" : \"test title\"\n" +
                " }\n" +
                "}");

        hub.sendNotification(n, tagname);