用于通知的 Amplify Swift 实用程序

Code Coverage

用于通知的 Amplify Swift 实用程序为在 iOS 和 macOS 上处理推送通知提供了有用的功能。

虽然它是为与 AWS Amplify 一起使用而开发的,但它也可以独立使用。

API 文档

特性

平台支持

用于通知的 Amplify Swift 实用程序包支持 iOS 13+ 和 macOS 10.15+。

许可证

此软件包在 Apache-2.0 许可证下获得许可。

安装

此软件包需要 Xcode 13.4 或更高版本才能构建。

Swift Package Manager

  1. Swift Package Manager 随 Xcode 一起分发。 要开始将此软件包添加到您的 iOS 项目,请在 Xcode 中打开您的项目并选择 File > Add Packagesadd-packages

  2. 在搜索栏中输入软件包 GitHub 存储库 URL (https://github.com/aws-amplify/amplify-swift-utils-notifications)。

  3. 您将看到 Swift Package Manager 要安装的版本的存储库规则。 选择 Up to Next Major Version 并输入 1.0.0 作为依赖项规则的最低版本,然后单击 Add Packageamplify-swift-utils-notifications

  4. 选择 AmplifyUtilsNotifications,然后单击 Add Package。

  5. 在您的应用程序代码中,根据需要显式导入插件。

    import SwiftUI
    import AmplifyUtilsNotifications
    
    @main
    struct HelloWorldApp: App {
        @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    class AppDelegate: NSObject, UIApplicationDelegate {
        func applicationDidFinishLaunching(_ application: UIApplication) {
            Task {
                let isPushNotificationAllowed = await AmplifyUtilsNotifications.AUNotificationPermissions.allowed
                // ...
            }
        }
    }

Cocoapods

  1. 此软件包也可通过 CocoaPods 获得。 如果您尚未安装 CocoaPods,请按照 此处的 说明进行操作。

  2. 将软件包作为依赖项添加到您的 Podfile。

    platform :ios, '13.0'
    use_frameworks!
    
    target 'HelloWorldApp' do
        pod 'AmplifyUtilsNotifications', '~> 1.0.0'
    end
  3. 然后运行以下命令

    pod install
  4. 使用 Xcode 打开 *.xcworkspace,您将能够在您的项目中使用 AmplifyUtilsNotifications 软件包。

AWS Pinpoint 的通知服务扩展

此软件包包含一个可以直接使用的实现 (AUNotificationService),用于处理由 AWS Pinpoint 发送的远程通知。 它有助于解码通知 json 数据并将远程媒体 URL 作为附件检索。

AWS Pinpoint 格式的推送通知

  1. 将服务应用程序扩展添加到您的项目。 Apple 文档

    add-notification-service-extension

  2. 更新新创建的通知服务扩展的 info.plist

    <dict>
        <key>NSExtension</key>
        <dict>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.usernotifications.service</string>
            <key>NSExtensionPrincipalClass</key>
            <string>AmplifyUtilsNotifications.AUNotificationService</string>
        </dict>
    </dict>

    注意

    我们建议您保留自动生成的 NotificationService.swift 源文件,或者为您的通知服务扩展目标添加一个空的 swift 文件。 当您尝试将扩展安装到真实设备时,空的源文件列表会导致错误。

非 AWS Pinpoint 格式的推送通知

您还可以继承 AUNotificationService 以支持不同的通知负载格式或添加自定义功能。

例如,我们要发送带有字段名称 video_url 的推送通知。

  1. 定义一个符合 AUNotificationPayload 协议的 MyPayload 结构体。 它定义了 remoteMediaURL

  2. 继承 AUNotificationService 并将 payloadSchema 属性更改为上一步中定义的 MyPayload

    import AmplifyUtilsNotifications
    
    struct MyPayload: AUNotificationPayload {
        var remoteMediaURL: String? {
            video_url
        }
    
        let video_url: String
    }
    
    class NotificationService: AUNotificationService {
    
        override init() {
            super.init()
            self.payloadSchema = MyPayload.self
        }
    }
  3. 通过将 NSExtensionPrincipalClass 设置为 $(PRODUCT_MODULE_NAME).NotificationService 来更新 info.plist

您还可以覆盖 didReceive 函数以根据需要修改内容。 例如,将后缀 [MODIFIED] 附加到您的通知标题。

override func didReceive(
    _ request: UNNotificationRequest,
    withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {
    super.didReceive(request) { content in
        self.contentHandler = contentHandler
        let mutableContent = content.mutableCopy() as? UNMutableNotificationContent
        mutableContent?.title = content.title + "[MODIFIED]"
        if let mutableContent {
            contentHandler(mutableContent)
        }
    }
}

报告错误/功能请求

Open Bugs Open Questions Feature Requests Closed Issues

我们欢迎您使用 GitHub 问题跟踪器来报告错误或建议功能。

提交问题时,请检查现有打开最近关闭的问题,以确保其他人尚未报告该问题。 请尝试包含尽可能多的信息。 像这样的细节非常有用