Git Product home page Git Product logo

aws-amplify / amplify-swift-utils-notifications Goto Github PK

View Code? Open in Web Editor NEW
3.0 30.0 3.0 579 KB

This repository serves as a dependency package for amplify-swift, providing valuable functionalities to handle push notifications on iOS and macOS platforms.

Home Page: https://aws-amplify.github.io/amplify-swift-utils-notifications/docs/

License: Apache License 2.0

Swift 75.51% Ruby 24.49%
aws aws-amplify ios push-notification swift

amplify-swift-utils-notifications's Introduction

Amplify Swift Utilities for Notifications

Amplify Swift Utilities for Notifications provides helpful functionality for working with push notifications on iOS and macOS.

Although it was developed for use with AWS Amplify, it can also be used independently.

API Documentation

Features

  • Convenience methods to support requesting notification permissions and registering with APNs
  • Push Notification Service extension to support fetching and attaching remote media to notifications

Platform Support

Amplify Swift Utilities for Notifications package supports iOS 13+ and macOS 10.15+.

License

This package is licensed under the Apache-2.0 License.

Installation

This package requires Xcode 13.4 or higher to build.

Swift Package Manager

  1. Swift Package Manager is distributed with Xcode. To start adding this package to your iOS project, open your project in Xcode and select File > Add Packages. add-packages

  2. Enter the package GitHub repo URL (https://github.com/aws-amplify/amplify-swift-utils-notifications) into the search bar.

  3. You'll see the repository rules for which version you want Swift Package Manager to install. Choose Up to Next Major Version and enter 1.0.0 as the minimum version for the Dependency Rule, then click Add Package. amplify-swift-utils-notifications

  4. Select AmplifyUtilsNotifications, then click Add Package.

  5. In your app code, explicitly import the plugin as needed.

    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. This package is also available through CocoaPods. If you have not installed CocoaPods, follow the instructions here.

  2. Add the package as a dependency to your Podfile.

    platform :ios, '13.0'
    use_frameworks!
    
    target 'HelloWorldApp' do
        pod 'AmplifyUtilsNotifications', '~> 1.0.0'
    end
  3. Then run the following command:

    pod install
  4. Open up *.xcworkspace with Xcode, and you will be able to use the AmplifyUtilsNotifications package in your project.

Notification Service Extension for AWS Pinpoint

This package includes a ready to use implementation(AUNotificationService) for handling remote notifications sent by AWS Pinpoint. It helps with decoding notification json data and retrieving the remote media url as an attachment.

Push notification in AWS Pinpoint format

  1. Add a Service App Extension to Your Project. Apple Doc.

    add-notification-service-extension

  2. Update info.plist of the newly created Notification Service Extension.

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

    Note:

    We suggest you either keep the auto generated NotificationService.swift source file or add an empty swift file for your Notification Service Extension target. An empty source list will cause an error when you try to install the extension to a real device.

Push notification not in AWS Pinpoint format

You can also subclass AUNotificationService to support a different notification payload format or add custom functionality.

For example, we want to send the push notification with a field name video_url.

  1. Define a MyPayload struct that conforms to the AUNotificationPayload protocol. It defines the remoteMediaURL.

  2. Subclass AUNotificationService and change the payloadSchema property to MyPayload that was defined in the previous step.

    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. Update the info.plist by setting NSExtensionPrincipalClass to $(PRODUCT_MODULE_NAME).NotificationService.

You can also override the didReceive function to modify the content as desired. For example, attach suffix [MODIFIED] to your notification title.

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)
        }
    }
}

Reporting Bugs/Feature Requests

Open Bugs Open Questions Feature Requests Closed Issues

We welcome you to use the GitHub issue tracker to report bugs or suggest features.

When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already reported the issue. Please try to include as much information as you can. Details like these are incredibly useful:

  • Expected behavior and observed behavior
  • A reproducible test case or series of steps
  • The version of our code being used
  • Any modifications you've made relevant to the bug
  • Anything custom about your environment or deployment

amplify-swift-utils-notifications's People

Contributors

5d avatar amazon-auto avatar ameter avatar dependabot[bot] avatar jcjimenez avatar phantumcode avatar ruisebas avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.