Push Notification
Castled provides a convenient way to send push notifications to your applications directly from our dashboard. Follow these steps to send your first push notification to your application:
Prerequisite
To enable push notifications on your iOS application using the Castled dashboard, you are required to upload the APNS Authentication Key obtained from your Apple Developer account.
Refer here for more information on how to obtain .p8 file.
Configuring App Target for Push Notification
To enable push notifications for your iOS app, you need to configure the app’s target settings in Xcode. This involves enabling the push notifications capability, enabling remote notifications, and adding an App Group ID.
Follow the steps below to set up these features in the Signing & Capabilities tab of your app target:
1. Enabling Push Notifications Capability
To receive push notifications, you must explicitly enable this capability.To enable push notification :
- Open your project in Xcode and select the app target.
- Go to the Signing & Capabilities tab.
- Click on the ”+ Capability” button to add a new capability.
- Search for “Push Notifications” and add it.
2. Enabling Remote Notifications
Remote notifications are essential for receiving notifications from a server.This will allow your app to be awakened in the background to receive notifications.
To enable Remote notifications :
- Within the Signing & Capabilities tab, ensure that the Background Modes capability is present.
- If not, add it by clicking the ”+ Capability” button and selecting “Background Modes.”
- In the Background Modes section, find and check the “Remote notifications” option.
It is advised to enable Remote Notifications for efficient Uninstall tracking and better Push impressions, even when the app is in the background.
3. Adding an App Group ID
App Groups facilitate the sharing of data between multiple apps developed by the same organization and their extensions. While this is not required specifically for push notifications, it is relevant if you have any notification-related extensions for rich notifications or need to manage a shared container of data.
To add an App Group:
- Still on the Signing & Capabilities tab, click the ”+ Capability” button to add a new capability.
- Type “App Groups” in the search bar and select it from the list.
- Under the App Groups section, click the ”+” to create a new group.
- Enter the App Group ID using the format group.your_bundle_id.castled.
- Make sure the App Group ID is checked to enable it.
To ensure proper integration, we recommend setting up a separate App Group ID specifically for Castled.The format should be group.your_bundle_id.castled. Make sure to enable this same App Group ID for all targets where Castled is utilized.
Registering Push Notification
In iOS 10 and above, to handle push notifications, you need to implement the following method from UIApplicationDelegate protocol and add the three steps mentioned in the code snippet below for registering push notification.
application(_:didFinishLaunchingWithOptions:)
This method is called when the app finishes launching. You can use this method to register for remote notifications and configure the notification settings.
Ensure the appGroupId mentioned above matches the one enabled in the App Groups section of the Signing & Capabilities tab for all relevant targets in Xcode, including extensions. If App Groups are not enabled, there is no need to set an appGroupId.
We recommend configuring the appGroupId and using the Notification Service Extension for enhanced tracking of notification impressions, accurate badge counts, and media support.
Requesting User Permission for Push Notifications
Obtaining user authorization is a crucial requirement for sending push notifications to an iOS application. When your app requests authorization for the first time, the system presents a prompt to the user, giving them the choice to grant or deny the request. The user’s response is then recorded by the system for future reference.
AppDelegate Swizzling in Castled SDK
Castled SDK is designed to simplify push notification handling. By default, swizzling is enabled, which means the SDK automatically handles the registration of device tokens, the presentation of notifications, and the response to user interactions with those notifications.
If you prefer to have more granular control over notification handling or need to comply with specific architectural requirements, disable swizzling by referring to the section below.
Disabling AppDelegate Swizzling
To disable method swizzling with Castled SDK, you must update the Info.plist file of your application. Add a new key-value pair where the key is specific to the Castled SDK’s configuration and the value explicitly indicates that swizzling should be turned off.
Refer to this section for instructions on how to disable swizzling in the Castled SDK, as well as how to manually handle push notifications afterwards.
Custom handling of User Actions
The Castled SDK provides support for handling custom notification actions when the user interacts with push notifications in your application. By implementing the CastledNotificationDelegate protocol in your AppDelegate class, you can define the behavior for different notification actions such as deeplinking, navigating to a specific screen, or displaying a rich landing page.
CastledNotificationDelegate method
notificationClicked This delegate method is invoked when a user clicks on a notification that was displayed as a result of a push notification sent from the Castled SDK. It provides detailed context about the notification that was clicked, allowing the application to handle different actions appropriately.
Parameters:
type : A CastledNotificationType indicating the type of notification. The values include ‘push’ or ‘inapp’.
CastledButtonAction : An optional type providing additional information associated with the notification, such as a URL to open or title to display,click action type etc…
buttonAction.actionType : A CastledClickActionType representing the action taken by the user. The values include:
- deepLink
- navigateToScreen
- richLanding
- requestForPush
- dismiss
- custom
userInfo : A dictionary containing the original notification payload. This includes the custom data that was sent with the notification for the application to use.
Configuring Notification Categories and Actions
Apps are required to enable support for actionable notifications. During app launch, it is necessary to register one or more notification categories that define the types of notifications your app sends. Each category is associated with specific actions that users can take when receiving a notification of that type.
While each category can have up to four actions, the actual number of displayed actions may vary depending on the notification display. For instance, banners typically show a maximum of two actions.
Registering the Notification Categories for Your App
By passing the notificationCategories parameter, you can define a set of categories and associated actions for your notifications. Each category corresponds to a type of notification that your app can receive, and for each category, you can specify the actions that a user can take directly from the notification.
This flexibility allows app developers to create a rich and interactive notification experience, tailored to the needs of their application and its users.
To report dismiss events for push notifications, include the customDismissAction in the UNNotificationCategory options. Make sure to define your notification categories with this option, as shown in the code snippet above.
The UNNotificationActionOptions.foreground in swift and UNNotificationActionOptionForeground in Objective C option is used to specify the behavior of a notification action when it is clicked by the user. Make sure this option is included, so that the action launches the application and bring it to the foreground.