Skip to content

Commit 0a20a47

Browse files
Update unreal iOS notifications page
1 parent 4447c64 commit 0a20a47

1 file changed

Lines changed: 99 additions & 89 deletions

File tree

docs/sdkx-unreal/notifications-ios.mdx

Lines changed: 99 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,127 @@ 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+
`RegisterPushTokenIos()` API call inside the application delegate method
69+
`application:didRegisterForRemoteNotificationsWithDeviceToken:` system method or `FCoreDelegates::ApplicationRegisteredForRemoteNotificationsDelegate` Unreal delegate, depending on how your game is structured. If using Unreal delegate, the best place to add this would be in the `GameInstance::OnStart()` method of your game instance class.
70+
71+
```cpp
72+
void UHelpshiftDemoGameInstance::OnStart()
73+
{
74+
Super::OnStart();
8475

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

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

96-
Add the below notification listeners on app launch. Preferably after Helpshift [Init](/sdkx-unreal/getting-started/#initializing)
86+
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.
9787

98-
```objc
99-
#include "HelpshiftSDK.h"
88+
```cpp
89+
void UHelpshiftDemoGameInstance::OnStart()
90+
{
91+
Super::OnStart();
10092

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

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)];
106+
### Implement push delegates {#push-delegates}
112107

113-
// Convert NSString to NSData
114-
NSData* jsonData = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
108+
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:
115109

116-
// Deserialize NSData to NSDictionary
117-
return [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
118-
}
110+
- If a notification arrives while your app is in the foreground, UNUserNotificationCenterDelegate's `userNotificationCenter:willPresentNotification:withCompletionHandler:` is called.
111+
- 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.
112+
- 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.
113+
114+
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:
119115

116+
Example usage:
117+
- For notifications sent in response to agent replies on user's support conversations, the SDK will open the corresponding conversation.
118+
- For proactive engagement notifications, the SDK will perform the dashboard configured action.
120119

121-
void HandleNotification(NSDictionary *payload,bool isAppLaunch) {
120+
#### Push delegate when app is in foreground {#push-delegates-foreground}
122121

123-
// Check if the "origin" key exists in the dictionary
124-
if ([payload objectForKey:@"origin"] != nil) {
125-
NSString *originValue = [payload objectForKey:@"origin"];
122+
For the `userNotificationCenter:willPresentNotification:withCompletionHandler:` delegate, invoke the `handleForegroundNotification:withCompletionHandler:` SDK API -
126123

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-
}
124+
```objc
125+
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
126+
willPresentNotification:(UNNotification *)notification
127+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions)) completionHandler {
128+
NSDictionary *userInfo = notification.request.content.userInfo;
129+
if([@"helpshift" isEqualToString:userInfo[@"origin"]]) {
130+
[Helpshift handleForegroundNotification:userInfo withCompletionHandler:completionHandler];
134131
} else {
135-
// app notifications
132+
completionHandler(UNNotificationPresentationOptionBanner |
133+
UNNotificationPresentationOptionSound |
134+
UNNotificationPresentationOptionList);
136135
}
137136
}
137+
```
138+
139+
#### Push delegate when app is in background or not running {#push-delegates-background}
138140

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

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;
143+
```objc
144+
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
145+
didReceiveNotificationResponse:(UNNotificationResponse *)response
146+
withCompletionHandler:(void (^)(void))completionHandler {
147+
NSDictionary *userInfo = response.notification.request.content.userInfo;
148+
if([@"helpshift" isEqualToString:userInfo[@"origin"]]) {
149+
[Helpshift handleBackgroundNotificationClick:userInfo withCompletionHandler:completionHandler];
150+
} else {
151+
completionHandler();
152+
}
153+
completionHandler();
144154
}
145-
// Handle notification for foreground & background state
146-
FCoreDelegates::ApplicationReceivedRemoteNotificationDelegate.AddStatic([](FString Json, int State) {
155+
```
147156

148-
NSDictionary* JsonDict = ConvertFStringToNSDictionary(Json);
157+
### Configure NotificationService extension {#notification-service}
149158

150-
bool IsAppLaunch = (State == 3) ? false : true;
159+
<Admonition type="info" title="Note">
151160

152-
if (JsonDict) {
153-
HandleNotification(JsonDict,IsAppLaunch);
154-
}
155-
});
161+
Your app only needs to configure the NotificationService extension if you are using Proactive Engagement feature. For support notifications, you can skip this configuration.
156162

157-
// Handle notification for killed state
158-
NSDictionary *LocalLaunchOptions = [IOSAppDelegate GetDelegate].launchOptions;
163+
</Admonition>
159164

160-
if (LocalLaunchOptions) {
161-
NSDictionary *Payload = LocalLaunchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
162-
HandleNotification(Payload,true);
163-
}
165+
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).
164166

165-
#endif
167+
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 -
168+
169+
```objc
170+
- (void) didReceiveNotificationRequest:(UNNotificationRequest *)request
171+
withContentHandler:(void (^)(UNNotificationContent *content))contentHandler {
172+
NSString *origin = request.content.userInfo[@"origin"];
173+
if([@"helpshift" isEqualToString:origin]) {
174+
[Helpshift handleBackgroundNotification:request
175+
withContentHandler:contentHandler];
176+
} else {
177+
contentHandler(request.content);
178+
}
179+
}
166180
```
167181

168182
## In-app notifications {#in-app-notifications}
@@ -171,16 +185,12 @@ In-app notifications, unlike push notifications, appear within your app when it
171185

172186
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.
173187

174-
<SideBySide>
175-
176188
<Image
177-
src="/static/books/sdkx_ios/In-app-notification.png"
189+
src="/static/books/sdkx_ios/support-inapp.png"
178190
width="half"
179-
alt="In-app-notification.png"
191+
alt="Support In-App Notification"
180192
/>
181193

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

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

0 commit comments

Comments
 (0)