Skip to content

Commit

Permalink
Add iOS implementation
Browse files Browse the repository at this point in the history
- Add JWT and event listener to wrapper
- Token added as parameter to login
- updateUserJwt
- UserJwtInvalidatedEvent
- Add & Remove Listener methods
- new methods updateUserJwt and addUserJwtInvalidatedListener added to the bridge
  • Loading branch information
jennantilla committed Dec 19, 2024
1 parent 0fa9e05 commit 3a0e386
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ios/RCTOneSignal/RCTOneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#import "../OneSignalFramework.h"
#endif

@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener, OSUserStateObserver>
@interface RCTOneSignal : NSObject <OSPushSubscriptionObserver, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener, OSUserStateObserver, OSUserJwtInvalidatedListener>
+ (RCTOneSignal *) sharedInstance;

@end
4 changes: 4 additions & 0 deletions ios/RCTOneSignal/RCTOneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ - (void)onDidDismissInAppMessage:(OSInAppMessageDidDismissEvent * _Nonnull)event
[self sendEvent:OSEventString(InAppMessageDidDismiss) withBody:[event jsonRepresentation]];
}

- (void)onUserJwtInvalidated:(OSUserJwtInvalidatedEvent * _Nonnull)event {
[self sendEvent:OSEventString(UserJwtInvalidated) withBody:[event jsonRepresentation]];
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Expand Down
3 changes: 2 additions & 1 deletion ios/RCTOneSignal/RCTOneSignalEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
InAppMessageDidDisplay,
InAppMessageWillDismiss,
InAppMessageDidDismiss,
UserJwtInvalidated,
};

#define OSNotificationEventTypesArray @[@"OneSignal-permissionChanged",@"OneSignal-subscriptionChanged",@"OneSignal-userStateChanged",@"OneSignal-notificationWillDisplayInForeground",@"OneSignal-notificationClicked",@"OneSignal-inAppMessageClicked", @"OneSignal-inAppMessageWillDisplay", @"OneSignal-inAppMessageDidDisplay", @"OneSignal-inAppMessageWillDismiss", @"OneSignal-inAppMessageDidDismiss"]
#define OSNotificationEventTypesArray @[@"OneSignal-permissionChanged",@"OneSignal-subscriptionChanged",@"OneSignal-userStateChanged",@"OneSignal-notificationWillDisplayInForeground",@"OneSignal-notificationClicked",@"OneSignal-inAppMessageClicked", @"OneSignal-inAppMessageWillDisplay", @"OneSignal-inAppMessageDidDisplay", @"OneSignal-inAppMessageWillDismiss", @"OneSignal-inAppMessageDidDismiss", @"OneSignal-userJwtInvalidated"]

#define OSEventString(enum) [OSNotificationEventTypesArray objectAtIndex:enum]

Expand Down
21 changes: 19 additions & 2 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation RCTOneSignalEventEmitter {
BOOL _hasAddedNotificationForegroundLifecycleListener;
BOOL _hasAddedInAppMessageClickListener;
BOOL _hasAddedInAppMessageLifecycleListener;
BOOL _hasAddedUserJwtInvalidatedListener;
NSMutableDictionary* _preventDefaultCache;
NSMutableDictionary* _notificationWillDisplayCache;
}
Expand Down Expand Up @@ -104,14 +105,30 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal initialize:appId withLaunchOptions:NULL];
}

RCT_EXPORT_METHOD(login:(NSString *)externalId) {
[OneSignal login:externalId];
RCT_EXPORT_METHOD(login:(NSString *)externalId jwtToken:(NSString *)jwtToken) {
// Pass nil if jwtToken is not provided
if (jwtToken == (id)[NSNull null]) {
jwtToken = nil;
}

[OneSignal login:externalId withToken:jwtToken];
}

RCT_EXPORT_METHOD(logout) {
[OneSignal logout];
}

RCT_EXPORT_METHOD(updateUserJwt:(NSString *)externalId token:(NSString *)token) {
[OneSignal updateUserJwt:externalId withToken:token];
}

RCT_EXPORT_METHOD(addUserJwtInvalidatedListener) {
if (!_hasAddedUserJwtInvalidatedListener) {
[OneSignal addUserJwtInvalidatedListener:[RCTOneSignal sharedInstance]];
_hasAddedUserJwtInvalidatedListener = true;
}
}

RCT_EXPORT_METHOD(enterLiveActivity:(NSString *)activityId
withToken:(NSString *)token
withResponse:(RCTResponseSenderBlock)callback) {
Expand Down
2 changes: 2 additions & 0 deletions src/events/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
IN_APP_MESSAGE_WILL_DISMISS,
IN_APP_MESSAGE_DID_DISMISS,
IN_APP_MESSAGE_DID_DISPLAY,
USER_JWT_INVALIDATED,
} from './events';
import OSNotification from '../OSNotification';

Expand All @@ -29,6 +30,7 @@ const eventList = [
IN_APP_MESSAGE_WILL_DISMISS,
IN_APP_MESSAGE_DID_DISMISS,
IN_APP_MESSAGE_DID_DISPLAY,
USER_JWT_INVALIDATED,
];

export default class EventManager {
Expand Down
1 change: 1 addition & 0 deletions src/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const IN_APP_MESSAGE_DID_DISMISS = 'OneSignal-inAppMessageDidDismiss';
export const PERMISSION_CHANGED = 'OneSignal-permissionChanged';
export const SUBSCRIPTION_CHANGED = 'OneSignal-subscriptionChanged';
export const USER_STATE_CHANGED = 'OneSignal-userStateChanged';
export const USER_JWT_INVALIDATED = 'OneSignal-userJwtInvalidated';
43 changes: 41 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
NOTIFICATION_WILL_DISPLAY,
PERMISSION_CHANGED,
SUBSCRIPTION_CHANGED,
USER_JWT_INVALIDATED,
USER_STATE_CHANGED,
} from './events/events';
import {
Expand Down Expand Up @@ -53,6 +54,10 @@ export enum LogLevel {
Verbose,
}

export interface UserJwtInvalidatedEvent {
externalId: string;
}

// Internal wrapper notification permission state that is being updated by the permission change handler.
let notificationPermission = false;

Expand Down Expand Up @@ -99,10 +104,10 @@ export namespace OneSignal {
* If your integration is user-centric, or you want the ability to identify the user beyond the current device, the
* login method should be called to identify the user.
*/
export function login(externalId: string) {
export function login(externalId: string, jwtToken?: string) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

RNOneSignal.login(externalId);
RNOneSignal.login(externalId, jwtToken || null);
}

/**
Expand All @@ -115,6 +120,40 @@ export namespace OneSignal {
RNOneSignal.logout();
}

/**
* Update the JWT token for a user.
*/
export function updateUserJwt(externalId: string, token: string) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

RNOneSignal.updateUserJwt(externalId, token);
}

/** Add a callback that fires when the user's JWT is invalidated. */
export function addEventListener(
event: 'userJwtInvalidated',
listener: (event: UserJwtInvalidatedEvent) => void,
) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

isValidCallback(listener);
RNOneSignal.addUserJwtInvalidatedListener();
eventManager.addEventListener<UserJwtInvalidatedEvent>(
USER_JWT_INVALIDATED,
listener,
);
}

/** Clears current UserJwtInvalidated listeners. */
export function removeEventListener(
event: 'userJwtInvalidated',
listener: (event: UserJwtInvalidatedEvent) => void,
) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

eventManager.removeEventListener(USER_JWT_INVALIDATED, listener);
}

/** For GDPR users, your application should call this method before setting the App ID. */
export function setConsentRequired(required: boolean) {
if (!isNativeModuleLoaded(RNOneSignal)) return;
Expand Down

0 comments on commit 3a0e386

Please sign in to comment.