📦 SwiftyLaunch Modules
🔔 NotifKit
Overview

NotifKit Module Overview

SwiftyLaunch Module - NotifKit

Powered by OneSignal

NotifKit wraps OneSignal's SDK to allow for an even easier integration of push notifications in your app.

After generating a project with NotifKit enabled and completing the initial project setup, you will get following features out of the box:

  • Ability to send push notifications to users: Easy push notifications. Read more.
  • Push Notification Permission Request: Ability to easily ask your users to allow you to send them push notifications. Read more.
  • Automatic routing to in-app notifications: Ability to show the push notifications as an in-app notification if the app is open. Read more.
  • (AnalyticsKit only) Automatic analytics tracking: Automatically track events related to the user interacting with push notifications. Read more.
  • (AuthKit only) Ability to use a shared user ID: If the user is signed in, the Firebase user ID is used as the alias ID for OneSignal. Read more.
  • (BackendKit only) BackendKit integration (Recommended): Send Push Notifications via Notifkit on your backend. Read more.

Module Deep Dive

Initialization and Configuration

Out of the box, on app launch NotifKit will:

  1. Initilize NotifKit in App.swift: by calling a static function PushNotifications.initOneSignal()
  2. Configure the UNNotificationServiceExtension (opens in a new tab).
  3. Clear all notification badges when the app is opened by calling OneSignal.Notifications.clearAll()
  4. Add a foreground notification observer to track when the user interacts with a notification and a notification permission observer to track when the user changes the notification permission.
  5. Add a notification lifecycle listener to route push notifications to in-app notifications if the app is open.
  6. Only when generated with AuthKit: Will observe if the user has signed into an account, and use the Firebase user ID as the alias ID for OneSignal, so the user can be accurately tracked across different providers via PushNotifications.associateUserWithID(USER_ID). We remove that alias when the user signes out by calling PushNotifications.removeUserIDAssociation().

General Usage

The PushNotifications object and its functions are designed to be used a static object throughout the application without having to store a persistant reference to it. That means you do not have to initialize it and simply call its functions by using PushNotifications.FUNCTION_NAME.

You can find the most relevent app-side functions in the Asking for Permissions to send Push Notifications section.

You can read about sending push notifications here: Sending Push Notifications.

AuthKit Integration

If AuthKit is selected during project generation, your app will automatically add Firebase User ID as an alias to the OneSignal user ID when the user signes in. This allows you to send push notifications on the OneSignal dashboard or from the the backend (using BackendKit) by simply using the Firebase User ID.

AnalyticsKit Integration

If AnalyticsKit is selected during project generation, events regarding user interacting with push notifications will automatically be tracked with notif as the source (both client-side and server-side).

Tracked events include:

  • Push notification permission state changes to 'OPTED_IN' or 'OPTED_OUT'
  • User interacts with a push notification
  • Added firebase user ID as an alias to the OneSignal user ID

Module File Contents

Let's go through every file that is contained in the NotifKit module:

Config

Contains the OneSignal-Info.plist file that you will paste your OneSignal App ID into.

OneSignalNotificationServiceExtension

Contains the UNNotificationServiceExtension (opens in a new tab) for the app.

Sources

Model / PushNotifications.swift

Contains the PushNotifications class, which is the entry point to interact with the module.

ViewModifier / ShowPushNotificationPermissionSheetIfNeededModifier.swift

Exposes the ShowPushNotificationPermissionSheetIfNeededModifier() view modifier that is attached to the root of the application that will show a sheet asking the user to allow push notifications if we do not have that permission yet. That sheet is shown when the PushNotifications.showNotificationsPermissionsSheet() function is called. Read more about this in Asking for Permission to send Push Notifications.

View / NotificationsPermissionView.swift

The view that is shown on the permission sheet and in the notification settings view when the user has not given us the permission to send them push notifications.

View / NotificationsSettingsView.swift

View that can contain notifications-related settings in the app. By default doesn't contain any settings, and shows the NotificationsPermissionView if the user has not given us the permission to send them push notifications.