Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ of this software and associated documentation files (the "Software"), to deal
import com.onesignal.notifications.INotificationLifecycleListener;
import com.onesignal.notifications.INotificationWillDisplayEvent;
import com.onesignal.notifications.IPermissionObserver;
import com.onesignal.notifications.internal.badges.impl.shortcutbadger.ShortcutBadgeException;
import com.onesignal.notifications.internal.badges.impl.shortcutbadger.ShortcutBadger;
import com.onesignal.user.state.IUserStateObserver;
import com.onesignal.user.state.UserChangedState;
import com.onesignal.user.subscriptions.IPushSubscription;
Expand Down Expand Up @@ -274,6 +276,15 @@ public void setPrivacyConsentRequired(Boolean required) {
OneSignal.setConsentRequired(required);
}

@ReactMethod
public void setBadgeCount(int count) {
try {
ShortcutBadger.applyCountOrThrow(mReactApplicationContext, count);
} catch (ShortcutBadgeException e) {
e.printStackTrace();
}
}

// OneSignal.Debug namespace methods
@ReactMethod
public void setLogLevel(int logLevel) {
Expand Down
19 changes: 19 additions & 0 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal setConsentRequired:required];
}

RCT_EXPORT_METHOD(setBadgeCount:(double)count) {
if (@available(iOS 16.0, macOS 10.13, macCatalyst 16.0, tvOS 16.0, visionOS 1.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setBadgeCount:count
withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"OneSignal: Could not setBadgeCount: %@", error);
}
}];
return;
} else {
// If count is 0, set to -1 instead to avoid notifications in tray being cleared
// this breaks in iOS 18, but at that point we're using the new setBadge API
NSInteger newCount = count == 0 ? -1 : count;
UIApplication *application = RCTSharedApplication();
[application setApplicationIconBadgeNumber:newCount];
}
}

// OneSignal.Debug namespace methods
RCT_EXPORT_METHOD(setLogLevel : (int)logLevel) {
[OneSignal.Debug setLogLevel:logLevel];
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ export namespace OneSignal {
RNOneSignal.setPrivacyConsentGiven(granted);
}

/**
* Ultility function to set badge count manually
*/
export function setBadgeCount(count: number) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

RNOneSignal.setBadgeCount(count);
}

export namespace Debug {
/**
* Enable logging to help debug if you run into an issue setting up OneSignal.
Expand Down Expand Up @@ -956,4 +965,4 @@ export {

export { default as OSNotification } from './OSNotification';
export type { InAppMessageClickResult } from './types/inAppMessage';
export type { NotificationClickResult } from './types/notificationEvents';
export type { NotificationClickResult } from './types/notificationEvents';