gpt4 book ai didi

java - FCM : setting the TTL ("time_to_live" or lifespan) attribute with a Java Server (Google documentation is wrong/unclear)

转载 作者:行者123 更新时间:2023-12-02 06:07:39 25 4
gpt4 key购买 nike

我已经搜索了几个小时,但找不到答案(除其他外,还有很多关于 SO 的未解答的问题)。

<小时/>

我当前的代码

/**
* Use when the server has determined how many Alerts are situated inside
* a single Monitored Zone of a user.
*
* Documentation:
* https://firebase.google.com/docs/cloud-messaging/admin/send-messages
*
* @param registrationToken tokens of the devices the notif is sent to
* @param mzName name of the Monitored Zone (can be its ID)
* @param nbrAlertes number of alerts detected inside the MZ
*/
public static void sendMzLiveAlertNotif(ArrayList<String> registrationToken,
String mzName, int nbrAlertes) {

if(registrationToken.size() == 0 || nbrAlertes == 0 || mzName.isEmpty())
return;

registrationToken.forEach(token -> {
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("title", NotifType.NEW_LIVE_ALERT.getTitle())
.putData("body", "\"" + mzName + "\" contient " + nbrAlertes + " alertes.")
.putData("tag", mzName)
.putData("nbrAlerts", nbrAlertes+"")
.setToken(token)
.build();

try {
// Send a message to the device.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
} catch (Exception e) {
e.printStackTrace();
}
});
}

这非常有效,直到我想为我的通知添加生命周期。我仍在尝试找出使用 Java 执行此操作的正确方法。

<小时/>

我的问题

我想知道如何为我的消息强加 30 小时的生命周期(以及为什么在我不使用 Google 似乎使用的 setAndroidConfig 方法的情况下它仍然有效)。

我的服务器是用 Java 编码的,通知被推送到 Android 应用程序。

我最初的想法是:

        Message message = Message.builder()
.putData("title", NotifType.NEW_LIVE_ALERT.getTitle())
.putData("body", "\"" + mzName + "\" contient " + nbrAlertes + " alertes.")
.putData("tag", mzName)
.putData("nbrAlerts", nbrAlertes+"")
.setToken(token)
.setAndroidConfig(AndroidConfig.builder()
.setTtl(3600 * 30) // 30 hours ?
.build())
.build();

...但是在了解 Google 如何使用 AndroidConfig 来完成整个事情之后,我想知道我是否也应该这样做。

<小时/>

Google 文档

我能找到的唯一例子来自 Google 本身。这是an example :

  @Test
public void testAndroidMessageWithoutNotification() throws IOException {
Message message = Message.builder()
.setAndroidConfig(AndroidConfig.builder()
.setCollapseKey("test-key")
.setPriority(Priority.HIGH)
.setTtl(10)
.setRestrictedPackageName("test-pkg-name")
.putData("k1", "v1")
.putAllData(ImmutableMap.of("k2", "v2", "k3", "v3"))
.build())
.setTopic("test-topic")
.build();
Map<String, Object> data = ImmutableMap.<String, Object>of(
"collapse_key", "test-key",
"priority", "high",
"ttl", "0.010000000s", // 10 ms = 10,000,000 ns
"restricted_package_name", "test-pkg-name",
"data", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3")
);
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "android", data), message);
}

您看到 .setTtl(10)"ttl", "0.010000000s",//10 ms = 10,000,000 ns 部分了吗?这让我很困惑。 Their documentation说(强调是我的):

time_to_live : Optional, number : This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live supported is 4 weeks, and the default value is 4 weeks. For more information, see Setting the lifespan of a message.

他们告诉我们阅读的链接说:

On Android and Web/JavaScript, you can specify the maximum lifespan of a message. The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message. Requests that don't contain this field default to the maximum period of four weeks.

他们显然希望事情能在几秒钟内完成。然而他们的测试显示使用了毫秒?!在他们的文档中发现这么多 JavaScript 示例,而几乎没有 Java 示例,真是令人沮丧!

就其本身而言,人们也可以发现这一矛盾 documentation :

public AndroidConfig.Builder setTtl (long ttl)

Sets the time-to-live duration of the message in milliseconds.

最佳答案

一个令人困惑的文档的好例子。

您应该选择 ms,因此 30 小时内您会得到如下结果:

        Message message = Message.builder()
.putData("title", NotifType.NEW_USER_ALERT.getTitle())
.putData("body", "\"" + mzName + "\" contient " + nbrAlertes + " alertes.")
.putData("tag", mzName)
.putData("nbrAlerts", nbrAlertes+"")
.setToken(token)
.setAndroidConfig(AndroidConfig.builder()
.setTtl(30*3600*1000) // 30 hours
.build())
.build();

关于java - FCM : setting the TTL ("time_to_live" or lifespan) attribute with a Java Server (Google documentation is wrong/unclear),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55932155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com