Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated example app to demo decrementing badge count #137

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion example/ios/KlaviyoReactNativeSdkExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
NSLog(@"URL is %@", url);
[RCTLinkingManager application:UIApplication.sharedApplication openURL: url options: @{}];
}];


// Installation Step 9a: update the app count to current badge number - 1. You can also set this to 0 if you
// no longer want the badge to show.
[PushNotificationsHelper updateBadgeCount: [UIApplication sharedApplication].applicationIconBadgeNumber - 1];

if (isDebug) {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"Push Notification"
Expand Down Expand Up @@ -113,6 +117,13 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
return [RCTLinkingManager application:app openURL:url options:options];
}

// Installation Step 14: if you want to reset the app badge count whenever the app becomes active implement this
// delegate method and set the badge count to 0. Note that this may sometimes mean that the user would miss the
// notification.
- (void)applicationDidBecomeActive:(UIApplication *)application {
[PushNotificationsHelper updateBadgeCount:0];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
return [self getBundleURL];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class PushNotificationsHelper: NSObject {
}
}

@objc
static func updateBadgeCount(_ count: Int) {
if #available(iOS 16.0, *) {
UNUserNotificationCenter.current().setBadgeCount(count)
} else {
UIApplication.shared.applicationIconBadgeNumber = count
}
}

@objc
static func setPushToken(token: Data) {
KlaviyoSDK().set(pushToken: token)
Expand Down