使用firebase-admin时设置FCM高优先级

时间:2018-04-01 19:25:34

标签: firebase firebase-cloud-messaging

我有以下代码,使用firebase-admin使用Firebase云消息发送消息

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);

上面的代码工作正常。但我需要发送“高优先级”消息,以便设备可以在打盹模式下接收它们。

如何将消息设为“高优先级”?

4 个答案:

答案 0 :(得分:1)

要发送到Android设备,在构建邮件时,请将其AndroidConfig设置为具有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();

有关其他详细信息,请参阅example in the documentation

发送至Apple设备时,请使用setApnsConfig(),如the documentation中所述。

答案 1 :(得分:1)

没有 AndroidConfig 构建器

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);
        });
}

答案 2 :(得分:0)

这可能会对某人有所帮助。

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);
    }

答案 3 :(得分:0)

message.Android = new AndroidConfig();
message.Android.Priority = Priority.High;