david
david

Reputation: 1077

set FCM high-priority when using firebase-admin

I have the following code which uses firebase-admin to send messages using Firebase cloud messaging

Message message = null;
message = Message.builder().putData("From", fromTel).putData("To", toTel).putData("Text", text)
            .setToken(registrationToken).build();

String response = null;
try {
    response = FirebaseMessaging.getInstance().sendAsync(message).get();
    responseEntity = new ResponseEntity<String>(HttpStatus.ACCEPTED);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
System.out.println("Successfully sent message: " + response);

The above code works fine. But I need to send "high-priority" messages so that the device can receive them while in doze mode.

How can I make the messages "high-priority"?

Upvotes: 6

Views: 11934

Answers (4)

Abdul Saleem
Abdul Saleem

Reputation: 10622

Without an AndroidConfig Builder

function sendFCM(token, from, to, text) {
    var admin = require("firebase-admin");
    var data = {
        from: from,
        to: to,
        text: text
    };
    let message = {       
        data: data,
        token: token,
        android: {
            priority: "high",  // Here goes priority
            ttl: 10 * 60 * 1000, // Time to live
        }
    };
    admin.messaging()
        .send(message)
        .then((response) => {
            // Do something with response
        }).catch((error) => {
            console.log(error);
        });
}

Upvotes: 3

Omar Kasabaki
Omar Kasabaki

Reputation: 21

public async Task send_PushNotification(FirebaseAdmin.Messaging.Message MESSAGE)
    {   
        var defaultApp = FirebaseApp.Create(new AppOptions()
        {
            Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "key_FB.json")),
        });

        var message = MESSAGE;
        message.Token = FB_TOKEN;
        message.Android = new AndroidConfig();
        message.Android.Priority = Priority.High;
        message.Android.TimeToLive = new TimeSpan(0,0,5);
       
        var messaging = FirebaseMessaging.DefaultInstance;
        var result = await messaging.SendAsync(message);
        Console.WriteLine(result);
    }

Upvotes: 2

viniciusalvess
viniciusalvess

Reputation: 814

This may help somebody.

public String sendFcmNotification(PushNotificationRequestDto notifyRequest) throws FirebaseMessagingException {
        String registrationToken = notifyRequest.getToken();

        AndroidConfig config = AndroidConfig.builder()
                .setPriority(AndroidConfig.Priority.HIGH).build();

        Notification notification = Notification.builder()
                .setTitle(notifyRequest.getTitle())
                .setBody(notifyRequest.getBody())
                .build();

        Message message = Message.builder()
                .setNotification(notification)
//                .putData("foo", "bar")
                .setAndroidConfig(config)
                .setToken(registrationToken)
                .build();


        return FirebaseMessaging.getInstance().send(message);
    }

Upvotes: 1

Bob Snyder
Bob Snyder

Reputation: 38299

For sending to Android devices, when building the message, set its AndroidConfig to a value that has Priority.HIGH:

AndroidConfig config = AndroidConfig.builder()
        .setPriority(AndroidConfig.Priority.HIGH).build();

Message message = null;
message = Message.builder()
        .putData("From", fromTel).putData("To", toTel).putData("Text", text)
        .setAndroidConfig(config) // <= ADDED
        .setToken(registrationToken).build();

For additional details, see the example in the documentation.

When sending to Apple devices, use setApnsConfig(), as explained in the documentation.

Upvotes: 6

Related Questions