Edit backend NotificatorFirebase.java

Fergal Powell5 years ago

I am running Traccar 4.1 on Ubuntu 18.04 hosted on my Droplet. I am using Firebase for push notifications to my custom Cordova mobile app. Push notifications are working however ios notifications have no sound. It is necessary for my mobile app to use a custom notification sound for any alerts so to achieve this I would need to change the Notification Class inside the NotificatorFirebase.java file in the backend to include a "sound" field which specifies the name of the sound file in the app. I also need to add a Data class which would contain the same information as the Notification class and include that in the message payload so that the notification contents are accesible in the foreground and background of app on ios and android as I learned from here: https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages

So I am hoping i could change the backend file to something like this below:

public class NotificatorFirebase extends Notificator {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorFirebase.class);

    private static final String URL = "https://fcm.googleapis.com/fcm/send";

    private String key;

    public static class Notification {
        @JsonProperty("body")
        private String body;
        @JsonProperty("sound")
        private String sound;
    }

public static class Data {
        @JsonProperty("body")
        private String body;
        @JsonProperty("sound")
        private String sound;
    }

    public static class Message {
        @JsonProperty("registration_ids")
        private String[] tokens;
        @JsonProperty("notification")
        private Notification notification;
        @JsonProperty("data")
        private Data data;
    }

    public NotificatorFirebase() {
        key = Context.getConfig().getString("notificator.firebase.key");
    }

    @Override
    public void sendSync(long userId, Event event, Position position) {
        final User user = Context.getPermissionsManager().getUser(userId);
        if (user.getAttributes().containsKey("notificationTokens")) {

            Notification notification = new Notification();
            notification.body = NotificationFormatter.formatShortMessage(userId, event, position).trim();
            notification.sound = "myCustomSound.wav";

            Data data = new Data();
            data.body = NotificationFormatter.formatShortMessage(userId, event, position).trim();
            data.sound = "myCustomSound.wav";

            Message message = new Message();
            message.tokens = user.getString("notificationTokens").split("[, ]");
            message.notification = notification;
            message.data = data;

            Context.getClient().target(URL).request()
                    .header("Authorization", "key=" + key)
                    .async().post(Entity.json(message), new InvocationCallback<Object>() {
                @Override
                public void completed(Object o) {
                }

                @Override
                public void failed(Throwable throwable) {
                    LOGGER.warn("Firebase notification error", throwable);
                }
            });
        }
    }

    @Override
    public void sendAsync(long userId, Event event, Position position) {
        sendSync(userId, event, position);
    }

}

I have checked out the Traccar repo using the following command: git clone --recursive https://github.com/traccar/traccar.git and attempted to open the Traccar maven project in NetBeans as instructed from here: https://www.traccar.org/build-in-netbeans/ however there is no maven project to open in that directory. So I opened the java file and edited as shown above and saved and ran mvn package which successfully built tracker-server.jar.

I initially installed Traccar 4.1 on my droplet by downloading it and running traccar.run file. If I replace the tracker-server.jar located in opt/tracar/ on my droplet with my newly created tracker-server.jar and then run traccar.run again will this use my updated backend? Do you see any problems with the changes I have made to NotificatorFirebase.java file? Anton if you are reading this, would you consider working with me on this for paid professional service if I can't manage to get this working on my own?

I appreciate any help and feedback on this. Thanks for reading!
Fergal

Karl.5 years ago

Fergal is your app usable by the public or is it an internal app only?

Thanks

Fergal Powell5 years ago

Hi Karl, my app will be open to the public. Thanks!

Macan5 years ago

So Fergal did you manage to add sound for the push messages?