Handling iOS push notifications
Usage guides to help you get started with the Push Notifications in the iOS Knock SDK.
Note: We Recommend taking advantage of our KnockAppDelegate to make managing your Push Notifications simpler.
Prerequisites
#Before proceeding, ensure you've configured push notifications within your Knock account. For guidance on this initial setup, refer to our Push Notification Configuration Guide.
Step 1: Enabling Push Notifications in Your App
#- Configure APNs in Your App:
- Open your project in Xcode.
- Navigate to your app target's Signing & Capabilities tab.
- Click the "+" capability button and add Push Notifications to enable Apple Push Notification service (APNs).
- Enable Background Modes:
- Still in the Signing & Capabilities tab, add the Background Modes capability.
- Check Remote notifications to allow your app to receive silent push notifications.
Step 2: Registering for Push Notifications
#Implement the following in your AppDelegate or SceneDelegate to register for push notifications:
KnockAppDelegate:
If using the KnockAppDelegate, this will be handled for you automatically.
Manually:
Step 3: Updating the Message Status of a Push Notification.
#If a push notification is sent via Knock, it will contain a knock_message_id property that includes the corresponding message Id. This can then be used to update the message status.
KnockAppDelegate:
If using the KnockAppDelegate, this will be handled for you automatically. When a user taps on a push notification, KnockAppDelegate automatically sets the message engagement status to "read" and "interacted".
Manually:
Step 4: Configuring Silent Push Notifications
#Silent push notifications allow your app to update content in the background without alerting the user. Ensure that your Knock APNs message template has silent notifications enabled.
KnockAppDelegate:
Manually:
Full Example
#Here's the complete AppDelegate implementation:
Using FCM in iOS
#If you prefer to use Firebase Cloud Messaging (FCM) within your iOS app, you can follow the same steps outlined above for push notifications with a few modifications to your AppDelegate.
Step 1: Configure Firebase
#In your application(_:didFinishLaunchingWithOptions:) method, add the following:
-
Initialize Firebase:
-
Set the Messaging Delegate:
-
Setup Knock:
Step 2: Override Push Notification Registration Methods
#Override the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method to prevent the KnockAppDelegate from handling this automatically:
Step 3: Implement MessagingDelegate
#Extend your AppDelegate to conform to MessagingDelegate and implement the messaging(_:didReceiveRegistrationToken:) method:
Full Example
#Here's the complete AppDelegate implementation with FCM support: