java - 如何从 (Java) Date 对象中获取 Expiry 字段以发送到 Apple Push Notification Services?

标签 java ios apple-push-notifications

任何人都可以阐明增强型通知格式(用于发送到 Apple 推送通知服务)的到期字段中的 4 个字节是什么意思,就像使用 Java 与 APNS 服务器通信一样?比如说,如果我准备 Date 对象,我如何将它变成发送到 APNS 所需的 4 个字节?

最佳答案

APNS 文档指出:

Expiry—A fixed UNIX epoch date expressed in seconds (UTC) that identifies when the notification is no longer valid and can be discarded. The expiry value should be in network order (big endian). If the expiry value is positive, APNs tries to deliver the notification at least once. You can specify zero or a value less than zero to request that APNs not store the notification at all.

如果您有一个 Date 对象(我们称之为 date ),它包含您的消息应该过期的日期/时间,发送到 APNS 的过期的 4 个字节是 expiry 的值:

Calendar cal = Calendar.getInstance ();
cal.setTime (date);
int expiry   = cal.getTimeInMillis () / 1000L;

或者你可以有一个相对的过期时间间隔。例如,如果您希望消息在发送到 APNS 服务器后 2 小时(7200 秒)过期,则 expiry 的值将是:

int expiry = (System.currentTimeMillis () / 1000L) + 7200;

关于java - 如何从 (Java) Date 对象中获取 Expiry 字段以发送到 Apple Push Notification Services?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380482/

相关文章:

java - 如何实现Comparable接口(interface)以及为什么要实现它

ios - 创建 ViewController 的最佳方式

ios - 我应该如何处理通过多个 channel 分发的应用程序的推送通知?

iOS 应用程序每次都会收到新的 APNS 设备 token

IOS 推送通知不适用于使用 firebase 和 react-native-push-notifications react 原生

Java 接口(interface)扩展 Comparable

java - 我们可以从 onOptionsItemSelected() click 中获取 Layout 中定义的按钮的 onClickListener 功能吗

java - 将接口(interface)实现为匿名内部类时出错

ios - 在 ios 上使用 typescript/web 音频 api

ios - 视频不再录制音频 (PBJVision/AVFoundation)