IBM Cloud 推送通知服务提供了一个统一的推送服务,用于向移动和 Web 应用程序发送实时通知。 Swift 服务器端 SDK 用于通过 IBM Cloud 推送通知服务发送推送通知。
在开始之前,请务必阅读IBM Cloud 推送通知服务文档。
import PackageDescription
let package = Package(
dependencies: [
.package(url: "https://github.com/ibm-bluemix-mobile-services/bluemix-pushnotifications-swift-sdk.git", .upToNextMajor(from: "1.6.0"))
]
)
sudo apt-get update // not required on Mac
swift build -Xcc -fblocks -Xlinker -rpath -Xlinker $(pwd)/.build/debug/
完成以下步骤
导入 IBMPushNotifications
框架。
import IBMPushNotifications
注意:对于联合使用,在初始化
PushNotifications
之前使用PushNotifications
的overrideServerHost
参数。 例如:PushNotifications.overrideServerHost = "https://syndicated.region.net"
使用 ApiKey 初始化
//Initialize PushNotifications
let myPushNotifications = PushNotifications(pushApiKey:"your-push-service-apiKey", pushAppGuid: "your-push-service-app_guid", pushRegion: PushNotifications.Region.US_SOUTH)
// GET AUTH TOKEN
myPushNotifications?.getAuthToken(completionHandler: { (hasToken, tokenString) in
if hasToken! {
// Send push notifications
}
})
使用 AppSecret 初始化
let myPushNotifications = PushNotifications(pushRegion: PushNotifications.Region.US_SOUTH, pushAppGuid: "your-push-service-guid", pushAppSecret: "your-push-service-appSecret")
创建一个简单的推送通知,将广播到所有设备。
let messageExample = Notification.Message(alert: "Testing IBMPushNotifications")
let notificationExample = Notification(message: messageExample)
使用以下方法发送推送通知
myPushNotifications.send(notification: notificationExample) { (data, status, error) in
if error != nil {
print("Failed to send push notification. Error: \(error!)")
}
}
注意:如果您使用 APIKEY 进行初始化,请在发送任何通知之前调用
getAuthToken()
。 这将为请求添加 Authorization 标头。
要创建更具选择性的推送通知,其中包含指定的设置,并且仅通过 deviceIds
或 userIds
或通过设备平台或基于标签订阅发送到某些设备,或者设置 GCM 和 APNs 功能 - 可以在相应的初始化程序中使用可选参数。
要发送以下批量推送通知:
myPushNotifications.sendBulk(notification: [notificationExample,notificationExample1,notificationExample2]) { (data, status, error) in
if error != nil {
print("Failed to send push notification. Error: \(error!)")
}
}
在 Target
中,传递以下值
deviceIds - 设备 ID 数组。
userIds - 用户 ID 数组。
platforms - 平台数组。
tagNames - 标签名称数组。
let targetExample = Notification.Target(deviceIds: ["device1","device2"],
userIds: ["userId1", "userId2"],
platforms: [TargetPlatform.Apple,TargetPlatform.Google,TargetPlatform.ChromeExtApp,
TargetPlatform.WebChrome,TargetPlatform.webFirefox,TargetPlatform.WebSafari], tagNames: ["tag1", "tag2"])
注意:不要一起使用 userIds、tagNames、platforms 和 deviceIds。
设置可以包含以下任何类型
APNs
GCM
FirefoxWeb
ChromeWeb
SafariWeb
ChromeAppExt
let notificationExample = Notification(message: messageExample,
apnsSettings: nil, gcmSettings: nil, firefoxWebSettings: nil,
chromeWebSettings: nil, safariWebSettings: nil, chromeAppExtSettings: nil)
APNs 设置可以包含以下参数
badge - 要显示为应用程序图标标记的数字
interactiveCategory - 用于交互式推送通知的类别标识符
iosActionKey - 操作键的标题
sound - 应用程序包中声音文件的名称。 将播放此文件的声音作为警报。
type - 通知类型:DEFAULT、MIXED 或 SILENT
payload - 将作为通知消息一部分发送的自定义 JSON 有效负载
titleLocKey - Localizable.strings
文件中当前本地化的标题字符串的键。 可以使用 %@ 和 %n$@ 说明符来格式化键字符串,以采用 titleLocArgs 数组中指定的变量。
locKey - Localizable.strings 文件中当前本地化(由用户的语言首选项设置)的警报消息字符串的键。 可以使用 %@ 和 %n$@ 说明符来格式化键字符串,以采用 locArgs 数组中指定的变量
launchImage - 应用程序包中图像文件的文件名,带有或不带有文件扩展名。 当用户点击操作按钮或移动操作滑块时,该图像用作启动图像
titleLocArgs - 变量字符串值,将出现在 title-loc-key 中格式说明符的位置
locArgs - 变量字符串值,将出现在 locKey 中格式说明符的位置。
title - 富推送通知的标题(仅在 iOS 10 及更高版本上受支持)
subtitle - 富通知的副标题。(仅在 iOS 10 及更高版本上受支持)
attachmentUrl - 指向 iOS 通知媒体(视频、音频、GIF、图像 - 仅在 iOS 10 及更高版本上受支持)的链接,
let apnsSetting = Notification.Settings.Apns(badge: 1, interactiveCategory: "Category",
iosActionKey: "VIEW", sound: "Newtune.wav", type: ApnsType.DEFAULT,
payload: ["key1":"value1"], titleLocKey: "TITLE1", locKey: "LOCKEY1",
launchImage: "launchImage1.png", titleLocArgs: ["arg1","arg2"],
locArgs: ["arg3","arg4"], title: "welcome to IBM Cloud Push service",
subtitle: "Push Notifications", attachmentUrl: "https://IBMCloud.net/image.png")
GCM 设置可以包含以下参数
collapseKey - 此参数标识一组消息。
delayWhileIdle - 当此参数设置为 true 时,表示在设备变为活动状态之前不应发送消息。
payload - 将作为通知消息的一部分发送的自定义 JSON 有效负载。
priority - 指示此通知优先级的字符串值。 允许的值为“max”、“high”、“default”、“low”和“min”。 高/最大优先级通知以及“sound”字段可用于 Android 5.0 或更高版本中的 Heads up 通知。 sampleval='low'。
sound - 当通知到达设备时将尝试播放的声音文件(在设备上)。
timeToLive - 如果设备处于脱机状态,此参数指定消息应在 GCM 存储中保留的时间(以秒为单位)。
interactiveCategory - 用于交互式推送通知的类别标识符。
icon - 指定要为通知显示的图标的名称。 确保该图标已与客户端应用程序打包在一起。
sync - 设备组消息传递使组中的每个应用程序实例都可以反映最新的消息传递状态。
visibility - private/public - 此通知的可见性,它会影响在安全锁定的屏幕上如何以及何时显示通知。
lights - 允许在接收推送通知时设置通知 LED 颜色。
style - 用于指定 Android 可扩展通知的选项。 可扩展通知的类型为 picture_notification
、bigtext_notification
、inbox_notification
。
type - 通知类型:DEFAULT 或 SILENT
androidTitle - 推送通知的标题
let lights = Notification.Settings.GcmLights(ledArgb: GcmLED.Green, ledOnMs: 3, ledOffMs: 3)
let style = Notification.Settings.GcmStyle(type: GcmStyleTypes.inbox_notification,
title: "inbox notification", url: "https://IBMCloud.net/image.png",
text: "some big text", lines: ["line 1","line 2"])
let gcmSettings = Notification.Settings.Gcm(androidTitle:"title", collapseKey: "collapseKey1", delayWhileIdle: false,
payload: ["key1":"value1"], priority: GcmPriority.DEFAULT,
sound: "sound.wav", timeToLive: 2,
interactiveCategory: "category1", icon: "icon.png",
sync: false, visibility: GcmVisibility.Public,
lights: lights, style: style,type: FCMType.DEFAULT)
FirefoxWeb
设置可以包含以下参数
title - 指定要为 WebPush 通知设置的标题。
iconUrl - 要为 WebPush 通知设置的图标的 URL。
payload - 将作为通知消息的一部分发送的自定义 JSON 有效负载。
timeToLive - 如果设备处于脱机状态,此参数指定消息应在 GCM 存储中保留的时间(以秒为单位)。
let firefoxSetttings = Notification.Settings.FirefoxWeb(title: "IBM Cloud Push Notifications",
iconUrl: "https://IBM Cloud.net/icon.png",
payload: ["key1":"value1"], timeToLive: 3)
ChromeWeb 设置可以包含以下参数
title - 指定要为 WebPush 通知设置的标题。
iconUrl - 要为 WebPush 通知设置的图标的 URL。
payload - 将作为通知消息的一部分发送的自定义 JSON 有效负载。
timeToLive - 如果设备处于脱机状态,此参数指定消息应在 GCM 存储中保留的时间(以秒为单位)。
let chromeSetttings = Notification.Settings.ChromeWeb(title: "IBM Cloud Push Notifications",
iconUrl: "https://IBMCloud.net/icon.png",
payload: ["key1":"value1"], timeToLive: 3)
SafariWeb 设置可以包含以下参数
let safariSettings = Notification.Settings.SafariWeb(title: "IBM Cloud Push Notifications", urlArgs: ["https://IBMCloud.net"], action: "View")
ChromeAppExt 设置可以包含以下参数
collapseKey - 此参数标识一组消息。
delayWhileIdle - 当此参数设置为 true 时,表示在设备变为活动状态之前不应发送消息。
title - 指定要为 WebPush 通知设置的标题。
iconUrl - 要为 WebPush 通知设置的图标的 URL。
timeToLive - 如果设备处于脱机状态,此参数指定消息应在 GCM 存储中保留的时间(以秒为单位)。
payload - 将作为通知消息的一部分发送的自定义 JSON 有效负载。
let chromeAppExtSettings = Notification.Settings.ChromeAppExt(title: "IBM Cloud Push Notifications", iconUrl: "https://IBMCloud.net/icon.png", collapseKey: "collapseKey1", delayWhileIdle: false, payload: ["key1":"value1"], timeToLive: 4)
在此处查找 API 文档 - https://ibm-bluemix-mobile-services.github.io/API-docs/server-SDK/IBMPushNotifications/index.html
有关示例,请访问 - Github 示例
有关视频教程,请访问 - IBM Cloud 推送通知
Twitter | YouTube | 博客 | Facebook |
======================= 版权所有 2020-21 IBM Corp.
根据 Apache License,版本 2.0(“License”)获得许可; 除非符合 License,否则您不得使用此文件。 您可以在以下位置获取 License 的副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则分发的软件是基于“按原样”的基础分发的,没有任何形式的明示或暗示的保证或条件。 请参阅 License 以了解管理权限和限制的具体语言。