Skip to content

Commit 580b9e8

Browse files
Update unreal iOS notifications page
1 parent 4447c64 commit 580b9e8

1 file changed

Lines changed: 96 additions & 89 deletions

File tree

docs/sdkx-unreal/notifications-ios.mdx

Lines changed: 96 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@ description: "Configure Push and In-app notifications."
66

77
import {
88
Admonition,
9-
CodeBlock,
10-
Tabs,
11-
TabItem,
12-
LatestSdkVersion,
139
Centered,
1410
Image,
15-
Intro,
16-
SideBySide,
17-
DownloadButton,
18-
Steps,
19-
Step,
11+
Intro
2012
} from "@site/src/components/forDocs";
2113

2214
import ConfigurePushNotificationsiOS from "@site/docs/_partials/_configurePushNotificationsiOS.mdx";
2315

24-
# Notifications <small>iOS</small> {#notifications}
16+
# Notifications iOS {#notifications}
2517

2618
<Intro>
2719

@@ -36,32 +28,22 @@ If you want to configure the notifications for Android - please refer [here](/sd
3628
</Admonition>
3729

3830
<Admonition type="info" title="Note">
39-
We have changed the notification flow starting from SDK version 10.3.0. Please review the
31+
We have changed the notification flow starting from SDK version 10.5.0. Please review the
4032
updated documentation below for details on the new notification process.
4133
</Admonition>
4234

43-
## Configure push notifications via Helpshift {#push-via-helpshift}
35+
Notifications can be sent to your users when:
4436

45-
Notifications can be sent to your users when you reply to an issue submitted by them. In addition to the expected Push Notification behavior on iOS, you can customize your notifications to display a numbered badge on your App icon or play a sound alert when a notification is received.
37+
- You reply to an issue submitted by them.
38+
- You send a [Proactive Engagement](/sdkx-unreal/proactive-engagement) notification targeting the user
4639

47-
If your app does not already use push, you will need to enable push for your app. To enable push notification in your application you need to add APNS registration code on app launch.
40+
Setting push notifications involves configuring both the Helpshift admin dashboard as well as the client app.
4841

49-
```objc
50-
#if PLATFORM_IOS
51-
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
52-
center.delegate = self;
53-
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
54-
completionHandler:^(BOOL granted, NSError *_Nullable error) {
55-
...
56-
}];
57-
[UIApplication sharedApplication registerForRemoteNotifications];
58-
...
59-
#endif
60-
```
42+
## Configure push notifications in the Helpshift admin dashboard {#configure-helpshift-push-admin}
6143

6244
<ConfigurePushNotificationsiOS />
6345

64-
### Configure the Helpshift Unreal SDK to handle notifications {#configure-helpshift-unreal-sdk}
46+
## Configure the Helpshift Unreal SDK to handle notifications {#configure-helpshift-unreal-sdk}
6547

6648
After you have configured the push notifications in the Helpshift console, you will need to add additional setup in the project, too. First of all, you should enable the Use Push Notifications toggle in the Project Settings:
6749

@@ -74,95 +56,124 @@ After you have configured the push notifications in the Helpshift console, you w
7456
<Admonition type="info" title="Note">
7557

7658
When push is not configured, Helpshift SDK shows out-of-the-box "in-app notifications" for every message sent by Agents/Bots.
77-
You should call `registerDeviceToken` API only after you have [configured the Helpshift dashboard](#configure-helpshift-push-admin) for push notifications. Calling the `registerDeviceToken` API without configuring the Helpshift dashboard will stop showing out-of-the-box "in-app notifications" for the end users.
59+
You should call `registerDeviceToken` API only after you have [configured the Helpshift dashboard](#configure-helpshift-push-admin) for push notifications. Calling the `RegisterPushTokenIos()` API without configuring the Helpshift dashboard will stop showing out-of-the-box "in-app notifications" for the end users.
7860

7961
</Admonition>
8062

63+
### Request notification permission and register your app for push {#push-permission}
64+
8165
For Helpshift SDK to work with the Helpshift push notification service
82-
you will need to invoke the `registerDeviceToken:` api call inside the application delegate method
83-
`application:didRegisterForRemoteNotificationsWithDeviceToken:`
66+
you will need to invoke the `FIOSPlatformMisc::RegisterForRemoteNotifications()` Unreal method. This method handles requesting the user's permission to receive push notifications and registers the app for remote notifications. You should call this method at an appropriate point in your app's lifecycle, such as your GameInstance's `OnStart()` method.
67+
68+
```cpp
69+
void UHelpshiftDemoGameInstance::OnStart()
70+
{
71+
Super::OnStart();
8472

85-
```objc
8673
#if PLATFORM_IOS
87-
#import <HelpshiftX_Static/HelpshiftX.h>
88-
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
89-
[Helpshift registerDeviceToken:deviceToken];
90-
}
74+
// Register for remote push notifications. This internally calls the system
75+
// API that presents push permission prompt to the user.
76+
FIOSPlatformMisc::RegisterForRemoteNotifications();
9177
#endif
78+
}
9279
```
9380

94-
### Handling Push Notification {#handling-push-notification}
81+
### Pass registered push token to Helpshift {#push-pass-to-helpshift}
9582

96-
Add the below notification listeners on app launch. Preferably after Helpshift [Init](/sdkx-unreal/getting-started/#initializing)
83+
For Helpshift SDK to work with the Helpshift push notification service, you will need to invoke the `RegisterPushTokenIos()` API call inside `FCoreDelegates::ApplicationRegisteredForRemoteNotificationsDelegate` Unreal delegate.
9784

98-
```objc
99-
#include "HelpshiftSDK.h"
85+
```cpp
86+
void UHelpshiftDemoGameInstance::OnStart()
87+
{
88+
Super::OnStart();
10089

10190
#if PLATFORM_IOS
102-
#import <HelpshiftX_Static/HelpshiftX.h>
103-
#include "IOS/IOSAppDelegate.h"
104-
#include "Misc/CoreDelegates.h"
91+
// Listen for push token received delegate.
92+
FCoreDelegates::ApplicationRegisteredForRemoteNotificationsDelegate.AddLambda([](const TArray<uint8>& Token) {
93+
UHelpshiftLibrary::RegisterPushTokenIos(Token);
94+
});
95+
96+
// Register for remote push notifications. This internally calls the system
97+
// API that presents push permission prompt to the user.
98+
FIOSPlatformMisc::RegisterForRemoteNotifications();
10599
#endif
100+
}
101+
```
106102

107-
#if PLATFORM_IOS
108-
NSDictionary* ConvertFStringToNSDictionary(const FString& Json)
109-
{
110-
// Convert FString to NSString
111-
NSString* JsonString = [NSString stringWithUTF8String:TCHAR_TO_UTF8(*Json)];
103+
### Implement push delegates {#push-delegates}
112104

113-
// Convert NSString to NSData
114-
NSData* jsonData = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
105+
To respond to the delivery of notifications, you must implement a delegate for the shared UNUserNotificationCenter object. Your delegate object must conform to the UNUserNotificationCenterDelegate protocol, which the notification center uses to deliver notification information to your app:
115106

116-
// Deserialize NSData to NSDictionary
117-
return [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
118-
}
107+
- If a notification arrives while your app is in the foreground, UNUserNotificationCenterDelegate's `userNotificationCenter:willPresentNotification:withCompletionHandler:` is called.
108+
- If a notification arrives while your app is in background or terminated, the system **does not** call the `userNotificationCenter:willPresentNotification:withCompletionHandler:` method. Instead, the system alerts the user according to the information in the notification itself.
109+
- When the user selects an action from the notification interface, the system notifies your app of the user's choice. To receive responses, your delegate object must implement the `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` method.
119110

111+
In both cases, you should check the "origin" field of the notification dictionary and call the corresponding SDK APIs (mentioned below) if the origin is "helpshift". The Helpshift SDK will handle the notification transparently depending on it's payload:
120112

121-
void HandleNotification(NSDictionary *payload,bool isAppLaunch) {
113+
Example usage:
114+
- For notifications sent in response to agent replies on user's support conversations, the SDK will open the corresponding conversation.
115+
- For proactive engagement notifications, the SDK will perform the dashboard configured action.
122116

123-
// Check if the "origin" key exists in the dictionary
124-
if ([payload objectForKey:@"origin"] != nil) {
125-
NSString *originValue = [payload objectForKey:@"origin"];
117+
#### Push delegate when app is in foreground {#push-delegates-foreground}
126118

127-
// Now you can use the originValue as needed
128-
if ([originValue isEqualToString:@"helpshift"]) {
129-
// Handle the case when origin is "helpshift"
130-
[Helpshift handleNotificationWithUserInfoDictionary:payload isAppLaunch:isAppLaunch];
131-
} else {
132-
// Handle other cases
133-
}
119+
For the `userNotificationCenter:willPresentNotification:withCompletionHandler:` delegate, invoke the `handleForegroundNotification:withCompletionHandler:` SDK API -
120+
121+
```objc
122+
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
123+
willPresentNotification:(UNNotification *)notification
124+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions)) completionHandler {
125+
NSDictionary *userInfo = notification.request.content.userInfo;
126+
if([@"helpshift" isEqualToString:userInfo[@"origin"]]) {
127+
[Helpshift handleForegroundNotification:userInfo withCompletionHandler:completionHandler];
134128
} else {
135-
// app notifications
129+
completionHandler(UNNotificationPresentationOptionBanner |
130+
UNNotificationPresentationOptionSound |
131+
UNNotificationPresentationOptionList);
136132
}
137133
}
134+
```
135+
136+
#### Push delegate when app is in background or not running {#push-delegates-background}
138137

139-
UHelpshiftSettings* Settings = FHelpshiftSDKModule::Get().GetSettings();
138+
For the `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler` delegate, invoke the `handleBackgroundNotificationClick:withCompletionHandler:` SDK API -
140139

141-
if (!Settings->UsePushNotifications) {
142-
UE_LOG(LogTemp, Error, TEXT("Use Push Notifications is not enabled in the Project Settings. Will not register the token."));
143-
return;
140+
```objc
141+
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
142+
didReceiveNotificationResponse:(UNNotificationResponse *)response
143+
withCompletionHandler:(void (^)(void))completionHandler {
144+
NSDictionary *userInfo = response.notification.request.content.userInfo;
145+
if([@"helpshift" isEqualToString:userInfo[@"origin"]]) {
146+
[Helpshift handleBackgroundNotificationClick:userInfo withCompletionHandler:completionHandler];
147+
} else {
148+
completionHandler();
149+
}
150+
completionHandler();
144151
}
145-
// Handle notification for foreground & background state
146-
FCoreDelegates::ApplicationReceivedRemoteNotificationDelegate.AddStatic([](FString Json, int State) {
152+
```
153+
154+
### Configure NotificationService extension {#notification-service}
147155

148-
NSDictionary* JsonDict = ConvertFStringToNSDictionary(Json);
156+
<Admonition type="info" title="Note">
149157

150-
bool IsAppLaunch = (State == 3) ? false : true;
158+
Your app only needs to configure the NotificationService extension if you are using Proactive Engagement feature. For support notifications, you can skip this configuration.
151159

152-
if (JsonDict) {
153-
HandleNotification(JsonDict,IsAppLaunch);
154-
}
155-
});
160+
</Admonition>
156161

157-
// Handle notification for killed state
158-
NSDictionary *LocalLaunchOptions = [IOSAppDelegate GetDelegate].launchOptions;
162+
If you are using Helpshift's [Proactive Engagement](/sdkx-unreal/proactive-engagement) feature, you will need to add a NotificationService extension to your app. This extension lets the app intercept the notification and modify it's payload before iOS system posts the notification in system tray. For more details on what this service extension does and how to configure it in your app, you can refer the [Apple documentation](https://developer.apple.com/documentation/usernotifications/modifying-content-in-newly-delivered-notifications).
159163

160-
if (LocalLaunchOptions) {
161-
NSDictionary *Payload = LocalLaunchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
162-
HandleNotification(Payload,true);
163-
}
164+
Helpshift uses this extension to download images to display in the push notification. If your app already has a NotificationService extension, you can re-use it for handling Helpshift notifications. In the `didReceiveNotificationRequest:withContentHandler:` method of your notification service, call the corresponding Helpshift API -
164165

165-
#endif
166+
```objc
167+
- (void) didReceiveNotificationRequest:(UNNotificationRequest *)request
168+
withContentHandler:(void (^)(UNNotificationContent *content))contentHandler {
169+
NSString *origin = request.content.userInfo[@"origin"];
170+
if([@"helpshift" isEqualToString:origin]) {
171+
[Helpshift handleBackgroundNotification:request
172+
withContentHandler:contentHandler];
173+
} else {
174+
contentHandler(request.content);
175+
}
176+
}
166177
```
167178

168179
## In-app notifications {#in-app-notifications}
@@ -171,16 +182,12 @@ In-app notifications, unlike push notifications, appear within your app when it
171182

172183
These notifications are sent when an agent replies to a customer's issue. Your customers can click on these banners to go straight into the conversation screen.
173184

174-
<SideBySide>
175-
176185
<Image
177-
src="/static/books/sdkx_ios/In-app-notification.png"
186+
src="/static/books/sdkx_ios/support-inapp.png"
178187
width="half"
179-
alt="In-app-notification.png"
188+
alt="Support In-App Notification"
180189
/>
181190

182-
</SideBySide>
183-
184191
### Configuring In-app notifications {#optional-flags}
185192

186193
The Helpshift [Init](/sdkx-unreal/getting-started/) call supports flags for configuring SDK behaviour.

0 commit comments

Comments
 (0)