跳转到主要内容

OSNotification 载荷参考

本页面通过 OSNotification 类解释 OneSignal 推送通知载荷的结构和字段。在移动应用中接收或处理通知时请参考此文档。
推送通知载荷限制为 4096 字节。为防止截断,请将载荷保持在 3500 字节以下。additionalData 字段限制为 2048 字节。

在您的应用中访问 OSNotification

所有 OneSignal SDK 都会触发一个通知事件监听器,返回一个 OSNotification 对象。
  • Android: OneSignal.setNotificationWillShowInForegroundHandler(...)
  • iOS: notificationReceivedBlockUNNotificationServiceExtension
使用此对象访问通知标题、正文、数据和其他属性。 OSNotification 类提供在 SDK 通知事件监听器中可访问的所有通知载荷数据。它将原始的 OSNotificationOSNotificationPayload 类合并为一个基于 getter 的单一接口。

Android 字段

PropertyTypeDescription
getBody()String通知的正文文本。
getTitle()String通知的标题。
getLaunchURL()String点击通知时打开的 URL。
getNotificationId()StringOneSignal 通知 UUID。
getAdditionalData()JSONObject通过仪表板或 REST API 设置的自定义键值数据。最多 2048 字节。
getTemplateId()String模板 UUID,如果使用模板发送。
getAndroidNotificationId()intAndroid 原生通知 ID。
getLargeIcon()String大图标的 URL 或资源名称。
getSmallIcon()String小图标资源名称。
getSmallIconAccentColor()StringARGB 格式的图标强调色。
getSound()String播放的声音资源名称。
getCollapseId()String通知替换的折叠键。
getPriority()intAndroid 优先级(-2 到 2)。
getLedColor()StringARGB 格式的 LED 颜色。
getLockScreenVisibility()int锁屏可见性:1 = 公开0 = 私密-1 = 机密
getFromProjectNumber()String发送者项目编号。
getGroupedNotifications()List<OSNotification>包含在摘要中的通知。
getGroupKey()String摘要中使用的组键。
getGroupMessage()String摘要文本。
getBackgroundImageLayout()BackgroundImageLayout背景图像布局和文本颜色的对象。
getActionButtons()List<ActionButton>带有图标、文本和 ID 的操作按钮。
getRawPayload()String载荷的完整原始 JSON 字符串。

iOS 字段

PropertyTypeDescription
bodyNSString通知的正文文本。
titleNSString通知的标题。
launchURLNSString点击通知时打开的 URL。
notificationIdNSStringOneSignal 通知 UUID。
additionalDataDictionary通过仪表板或 REST API 设置的自定义键值数据。最多 2048 字节。
templateIdNSString模板 UUID,如果使用模板发送。
subtitleNSString副标题文本。
categoryNSStringiOS 类别标识符。
threadIdNSString用于将通知分组为线程(iOS 10+)。
badgeNSInteger绝对徽章值。
badgeIncrementNSInteger忽章增量数量。
contentAvailableBOOL如果 content-available=1,触发后台获取。
mutableContentBOOL如果 mutable-content=1,触发通知服务扩展。
actionButtonsNSArrayiOS 操作按钮。
rawPayloadNSDictionary载荷的完整原始 JSON。
parseWithApnsMethod将原始 APNS 载荷转换为 OSNotification。在服务扩展中使用。

OSNotificationAction(点击事件)

描述用户与通知的交互。
PropertyTypeDescription
actionIdString被点击的操作按钮的 ID。
typeenumOpened(默认点击)或 ActionTaken(按钮点击)。

OneSignal 自定义载荷结构

所有 OneSignal 通知在载荷中都包含一个特殊的 "custom" 对象:
{
  "custom": {
    "i": "the-notification-id"
  }
}
这个键是 OneSignal SDK 处理通知所必需的。如果缺少,通知将不会触发点击事件或分析。如果从不同的服务向已经使用 OneSignal 的设备发送推送,请避免重复通知。

可选:将 additionalData 移动到 APNS 根目录

对于 iOS 应用,您可以将 additionalData 放在 APNS 载荷的根目录中,以便在自定义处理程序中更容易访问。
  1. 在应用设置中启用 使用更新应用 API并设置:
{
  "additional_data_is_root_payload": true
}
  1. 使用 data 发送推送 它将在 APNS 载荷的根目录中可用。示例:
{
  "aps": {
    "alert": { "title": "Sale", "body": "20% off all items!" }
  },
  "promo_code": "SPRING20"
}
现在您可以直接访问 promo_code 而无需检查自定义字典。

恢复的通知

重启或应用重启后,Android SDK 将恢复通知。
属性类型描述
restoringboolean如果通知在设备/应用重启后被恢复,则为 true
可以使用 restoring 标志跳过恢复的通知。为避免恢复旧内容,请在您的通知上设置短或 0 TTL(生存时间)。

相关主题


I