Asking for Permission to send Push Notifications
We have built an easy way to ask for system permissions,
such as contacts and locations that you can envoke via a simple function, like askUserFor(.microphoneAccess)
to which you
can pass a closure that will be executed when the user has made their choice.
For Push Notifications we have built a similar system, which wraps around OneSignal's SDK to ask the user for permission to send them push notifications.
Checking if we have permission
To check if we have permission to send push notifications, you can use the PushNotifications.hasNotificationsPermission()
function.
public class PushNotifications {
static public func hasNotificationsPermission() async -> Bool { }
}
This function will return a Bool
value indicating whether the user has given us the consent to send them push notifications.
Showing a Permission Sheet
If we don't have the permission to send notifications, we can simply show a sheet
that will ask the user to give us the permission by calling PushNotifications.showNotificationsPermissionsSheet()
.
extension PushNotifications {
static public func showNotificationsPermissionsSheet() { }
}
If the user hasn't already given permission, a sheet will appear. The sheet will present two buttons to the user:
- Allow Notifications: Will show a system-level prompt asking the user to allow the app to send them push notifications.
- Dismiss: This will dismiss the sheet.
After the user presses on allow, the PushNotifications.askSystemPermission()
function will be called.
This will show a system-level prompt asking the user to allow the app to send them push notifications.
Because we can only show that prompt once, if the user declines the permission, we will not be able to ask them again.
This is why when we show the sheet, we first check if we can show the system prompt again, by calling PushNotifications.canAskForNotificationsPermission()
.
- If the
canAskForNotificationsPermission()
returns true, we show it as described above. - If the
canAskForNotificationsPermission()
returns false, we replace the "Allow Notifications" button with a "Allow in Settings" button, which redirects the users to the settings app with the app settings open.
Other Definitions
The above functions are the ones that are the most commonly used when asking for permission to send push notifications. Here are other functions that are used under the hood to achieve the behavior described above:
Have we already shown a system-level prompt that asks for permission?
Self-explanatory.
public class PushNotifications {
static public func canAskForNotificationsPermission() -> Bool { }
}
Show a system-level modal asking for permission
You can also call this function directly to show a system-level modal asking for the permission. We'd suggest asking for permission using the permission sheet instead, as we can show it as many times as we want and will only show the system-level modal when the user has already confirmed on the sheet.
If we don't have permission, this function opens the settings app with our app's settings open.
public class PushNotifications {
@MainActor
static public func askSystemPermission() async { }
}
Listener View Modifier attached to the root
The ShowPushNotificationPermissionSheetIfNeededModifier
is a view modifier that is attached to the root view which
listens when the showNotificationsPermissionsSheet()
function
public struct ShowPushNotificationPermissionSheetIfNeededModifier: ViewModifier { }
import SwiftUI
import NotifKit
// ...
@main
struct MainApp: App {
// ...
var body: some Scene {
// ...
ContentView()
// ...
.modifier(ShowPushNotificationPermissionSheetIfNeededModifier())
// ...
}
// ...
}
// ...