IBM Cloud 移动服务 - 用于 IBM Cloud 推送通知服务的服务器端 Swift SDK

Swift Platform Build Status Codacy Badge Coverage Status

IBM Cloud 推送通知服务提供了一个统一的推送服务,用于向移动和 Web 应用程序发送实时通知。 Swift 服务器端 SDK 用于通过 IBM Cloud 推送通知服务发送推送通知。

在开始之前,请务必阅读IBM Cloud 推送通知服务文档

内容

安装

Swift 包管理器

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"))
	]
)

在 Linux 上构建

sudo apt-get update // not required on Mac
swift build -Xcc -fblocks -Xlinker -rpath -Xlinker $(pwd)/.build/debug/

用法

简单通知

完成以下步骤

  1. 导入 IBMPushNotifications 框架。

    import IBMPushNotifications

注意:对于联合使用,在初始化 PushNotifications 之前使用 PushNotificationsoverrideServerHost 参数。 例如:PushNotifications.overrideServerHost = "https://syndicated.region.net"

  1. 使用有关 IBM Cloud 推送通知服务的详细信息进行初始化。
  1. 创建一个简单的推送通知,将广播到所有设备。

    let messageExample = Notification.Message(alert: "Testing IBMPushNotifications")
    let notificationExample = Notification(message: messageExample)
  2. 使用以下方法发送推送通知

    myPushNotifications.send(notification: notificationExample) { (data, status, error) in
      if error != nil {
        print("Failed to send push notification. Error: \(error!)")
      }
    }

注意:如果您使用 APIKEY 进行初始化,请在发送任何通知之前调用 getAuthToken()。 这将为请求添加 Authorization 标头。

要创建更具选择性的推送通知,其中包含指定的设置,并且仅通过 deviceIdsuserIds 或通过设备平台或基于标签订阅发送到某些设备,或者设置 GCM 和 APNs 功能 - 可以在相应的初始化程序中使用可选参数。

发送批量推送通知

要发送以下批量推送通知:

myPushNotifications.sendBulk(notification: [notificationExample,notificationExample1,notificationExample2]) { (data, status, error) in
	if error != nil {
		print("Failed to send push notification. Error: \(error!)")
	}
}

通知选项

目标

Target 中,传递以下值

注意:不要一起使用 userIds、tagNames、platforms 和 deviceIds。

设置

设置可以包含以下任何类型

APNs

APNs 设置可以包含以下参数

GCM

GCM 设置可以包含以下参数

FirefoxWeb

FirefoxWeb 设置可以包含以下参数

ChromeWeb

ChromeWeb 设置可以包含以下参数

SafariWeb

SafariWeb 设置可以包含以下参数

let safariSettings = Notification.Settings.SafariWeb(title: "IBM Cloud Push Notifications", urlArgs: ["https://IBMCloud.net"], action: "View")
ChromeAppExt

ChromeAppExt 设置可以包含以下参数

API 文档

在此处查找 API 文档 - https://ibm-bluemix-mobile-services.github.io/API-docs/server-SDK/IBMPushNotifications/index.html

示例和视频

了解更多

连接 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 以了解管理权限和限制的具体语言。