使用Firebase-Admin时,设置FCM高优先级



我有以下代码,该代码使用firebase-admin使用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);

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

如何使消息"高优先级"?

用于发送到Android设备时,构建消息时,将其AndroidConfig设置为具有优先级的值。高:

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

有关其他详细信息,请参见文档中的示例。

发送到Apple设备时,请使用setApnsConfig(),如文档中所述。

这可能会帮助某人。

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

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

相关内容

  • 没有找到相关文章

最新更新