Skip to content

Commit efa8695

Browse files
authored
feat(example/ios): differentiate silent from standard push with content-available (#333)
* feat(example/ios): differentiate pure silent push from standard push with content-available didReceiveRemoteNotification fires for both pure silent pushes and standard pushes with content-available. Add the delegate method with differentiation: only pure silent pushes (no aps.alert) are forwarded to PushNotificationsHelper.handleSilentPush for background processing. Standard pushes with content-available are left to willPresent/didReceive. Shows a debug alert when isDebug=YES, consistent with existing pattern. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * chore: add NSLog for content-avail push
1 parent d16eab9 commit efa8695

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

example/ios/KlaviyoReactNativeSdkExample/AppDelegate.mm

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,44 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
184184
[PushNotificationsHelper updateBadgeCount:0];
185185
}
186186

187+
// iOS Installation Step 15: Handle silent push notifications (content-available: 1).
188+
// This method fires for both pure silent pushes and standard pushes that carry
189+
// content-available. Only forward pure silent pushes (no aps.alert) for background
190+
// processing — standard pushes are handled by willPresent/didReceive.
191+
- (void)application:(UIApplication *)application
192+
didReceiveRemoteNotification:(NSDictionary *)userInfo
193+
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
194+
NSDictionary *apsPayload = userInfo[@"aps"];
195+
BOOL hasVisibleContent = apsPayload[@"alert"] != nil;
196+
197+
if (!hasVisibleContent) {
198+
[PushNotificationsHelper handleSilentPushWithUserInfo:userInfo];
199+
200+
if (isDebug) {
201+
NSString *payload = [NSString stringWithFormat:@"%@", userInfo];
202+
UIAlertController *alert = [UIAlertController
203+
alertControllerWithTitle:@"Silent Push Received"
204+
message:payload
205+
preferredStyle:UIAlertControllerStyleAlert];
206+
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
207+
style:UIAlertActionStyleDefault
208+
handler:nil]];
209+
[self.window.rootViewController presentViewController:alert
210+
animated:YES
211+
completion:nil];
212+
}
213+
} else if (apsPayload[@"content-available"]) {
214+
if (isDebug) {
215+
NSLog(@"Standard Push with Background Processing: %@", userInfo);
216+
}
217+
}
218+
219+
// You MUST call the completion handler within ~30 seconds.
220+
// Failing to do so will cause iOS to throttle or stop delivering
221+
// silent push notifications to your app.
222+
completionHandler(UIBackgroundFetchResultNewData);
223+
}
224+
187225
// iOS Installation Step 16: call this method from
188226
// `application:didFinishLaunchingWithOptions:` before calling the super class
189227
// method with the launch arguments. This is a workaround for a react issue

example/ios/KlaviyoReactNativeSdkExample/PushNotificationsHelper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class PushNotificationsHelper: NSObject {
4040
KlaviyoSDK().set(pushToken: token)
4141
}
4242

43+
@objc
44+
static func handleSilentPush(userInfo: NSDictionary) {
45+
NSLog("[Klaviyo] Silent push received: %@", userInfo)
46+
// Perform background work here, e.g. prefetch content or sync state.
47+
}
48+
4349
@objc
4450
static func handleReceivingPush(
4551
response: UNNotificationResponse,

0 commit comments

Comments
 (0)