diff --git a/example/IonicCapOneSignal/ios/App/Podfile.lock b/example/IonicCapOneSignal/ios/App/Podfile.lock index e1d1cec0..cbbf6afe 100644 --- a/example/IonicCapOneSignal/ios/App/Podfile.lock +++ b/example/IonicCapOneSignal/ios/App/Podfile.lock @@ -12,10 +12,10 @@ PODS: - Capacitor - CordovaPluginsStatic (6.0.0): - CapacitorCordova - - OneSignalXCFramework (= 5.2.9) - - OneSignalXCFramework (5.2.9): - - OneSignalXCFramework/OneSignalComplete (= 5.2.9) - - OneSignalXCFramework/OneSignal (5.2.9): + - OneSignalXCFramework (= 5.3.0-beta-01) + - OneSignalXCFramework (5.3.0-beta-01): + - OneSignalXCFramework/OneSignalComplete (= 5.3.0-beta-01) + - OneSignalXCFramework/OneSignal (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalExtension - OneSignalXCFramework/OneSignalLiveActivities @@ -23,38 +23,38 @@ PODS: - OneSignalXCFramework/OneSignalOSCore - OneSignalXCFramework/OneSignalOutcomes - OneSignalXCFramework/OneSignalUser - - OneSignalXCFramework/OneSignalComplete (5.2.9): + - OneSignalXCFramework/OneSignalComplete (5.3.0-beta-01): - OneSignalXCFramework/OneSignal - OneSignalXCFramework/OneSignalInAppMessages - OneSignalXCFramework/OneSignalLocation - - OneSignalXCFramework/OneSignalCore (5.2.9) - - OneSignalXCFramework/OneSignalExtension (5.2.9): + - OneSignalXCFramework/OneSignalCore (5.3.0-beta-01) + - OneSignalXCFramework/OneSignalExtension (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalOutcomes - - OneSignalXCFramework/OneSignalInAppMessages (5.2.9): + - OneSignalXCFramework/OneSignalInAppMessages (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalNotifications - OneSignalXCFramework/OneSignalOSCore - OneSignalXCFramework/OneSignalOutcomes - OneSignalXCFramework/OneSignalUser - - OneSignalXCFramework/OneSignalLiveActivities (5.2.9): + - OneSignalXCFramework/OneSignalLiveActivities (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalOSCore - OneSignalXCFramework/OneSignalUser - - OneSignalXCFramework/OneSignalLocation (5.2.9): + - OneSignalXCFramework/OneSignalLocation (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalNotifications - OneSignalXCFramework/OneSignalOSCore - OneSignalXCFramework/OneSignalUser - - OneSignalXCFramework/OneSignalNotifications (5.2.9): + - OneSignalXCFramework/OneSignalNotifications (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalExtension - OneSignalXCFramework/OneSignalOutcomes - - OneSignalXCFramework/OneSignalOSCore (5.2.9): + - OneSignalXCFramework/OneSignalOSCore (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - - OneSignalXCFramework/OneSignalOutcomes (5.2.9): + - OneSignalXCFramework/OneSignalOutcomes (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - - OneSignalXCFramework/OneSignalUser (5.2.9): + - OneSignalXCFramework/OneSignalUser (5.3.0-beta-01): - OneSignalXCFramework/OneSignalCore - OneSignalXCFramework/OneSignalNotifications - OneSignalXCFramework/OneSignalOSCore @@ -97,8 +97,8 @@ SPEC CHECKSUMS: CapacitorHaptics: 9ebc9363f0e9b8eb4295088a0b474530acf1859b CapacitorKeyboard: deacbd09d8d1029c3681197fb05d206b721d5f73 CapacitorStatusBar: 2e4369f99166125435641b1908d05f561eaba6f6 - CordovaPluginsStatic: 74720072a4a4ab131d56823ddd85736b6bc9f92b - OneSignalXCFramework: f5b2a3c4f130e4c910ead7bb25bed7455e976fbf + CordovaPluginsStatic: ec3f38153034076a0419390445f43486bdca03ce + OneSignalXCFramework: b859cf761f5437ff56a2a9fd158e67d74cd3bcfc PODFILE CHECKSUM: 178e2a2e451311a871c2b4db713ac4b63d0ebeeb diff --git a/plugin.xml b/plugin.xml index 75153593..49abcd4e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -85,7 +85,7 @@ - + diff --git a/src/ios/OneSignalPush.h b/src/ios/OneSignalPush.h index a2d0321b..78fad08f 100644 --- a/src/ios/OneSignalPush.h +++ b/src/ios/OneSignalPush.h @@ -31,7 +31,7 @@ #import -@interface OneSignalPush : CDVPlugin +@interface OneSignalPush : CDVPlugin - (void)setProvidesNotificationSettingsView:(CDVInvokedUrlCommand* _Nonnull)command; - (void)addForegroundLifecycleListener:(CDVInvokedUrlCommand* _Nonnull)command; @@ -48,6 +48,8 @@ - (void)login:(CDVInvokedUrlCommand* _Nonnull)command; - (void)logout:(CDVInvokedUrlCommand* _Nonnull)command; +- (void)updateUserJwt:(CDVInvokedUrlCommand* _Nonnull)command; +- (void)addUserJwtInvalidatedListener:(CDVInvokedUrlCommand* _Nonnull)command; - (void)addTags:(CDVInvokedUrlCommand* _Nonnull)command; - (void)removeTags:(CDVInvokedUrlCommand* _Nonnull)command; diff --git a/src/ios/OneSignalPush.m b/src/ios/OneSignalPush.m index 5b8739ab..915032bb 100644 --- a/src/ios/OneSignalPush.m +++ b/src/ios/OneSignalPush.m @@ -47,6 +47,7 @@ NSString* inAppMessageDidDismissCallbackId; NSString* inAppMessageClickedCallbackId; NSString* userObserverCallbackId; +NSString* addUserJwtInvalidatedListenerCallbackId; OSNotificationClickEvent *actionNotification; OSNotification *notification; @@ -367,13 +368,29 @@ - (void)setAlertLevel:(CDVInvokedUrlCommand*)command { } - (void)login:(CDVInvokedUrlCommand*)command { - [OneSignal login:command.arguments[0]]; + NSString *externalId = command.arguments[0]; + // check if jwt token provided + NSString *jwtToken = command.arguments.count > 1 ? command.arguments[1] : nil; + + [OneSignal login:externalId withToken:jwtToken]; } - (void)logout:(CDVInvokedUrlCommand*)command { [OneSignal logout]; } +- (void)updateUserJwt:(CDVInvokedUrlCommand*)command { + [OneSignal updateUserJwt:command.arguments[0] withToken:command.arguments[1]]; +} + +- (void)addUserJwtInvalidatedListener:(CDVInvokedUrlCommand*)command { + bool first = addUserJwtInvalidatedListenerCallbackId == nil; + addUserJwtInvalidatedListenerCallbackId = command.callbackId; + if (first) { + [OneSignal addUserJwtInvalidatedListener:self]; + } +} + - (void)addTags:(CDVInvokedUrlCommand*)command { [OneSignal.User addTags:command.arguments[0]]; } diff --git a/www/index.ts b/www/index.ts index 04a9f2c1..647f7844 100644 --- a/www/index.ts +++ b/www/index.ts @@ -36,6 +36,10 @@ import LiveActivities from "./LiveActivitiesNamespace"; // Suppress TS warnings about window.cordova declare let window: any; // turn off type checking +export interface UserJwtInvalidatedEvent { + externalId: string; +} + export class OneSignalPlugin { User: User = new User(); Debug: Debug = new Debug(); @@ -47,6 +51,14 @@ export class OneSignalPlugin { private _appID = ""; + private _userJwtInvalidatedEventListenerList: ((event:UserJwtInvalidatedEvent)=>void)[] = []; + + private _processFunctionList(array: ((event:any)=>void)[], param: any): void { + for (let i = 0; i < array.length; i++) { + array[i](param); + } + } + /** * Initializes the OneSignal SDK. This should be called during startup of the application. * @param {string} appId @@ -64,23 +76,59 @@ export class OneSignalPlugin { }; /** - * Login to OneSignal under the user identified by the [externalId] provided. The act of logging a user into the OneSignal SDK will switch the [user] context to that specific user. - * @param {string} externalId + * Log in to OneSignal under the user identified by the [externalId] provided. The act of logging a user into the OneSignal SDK will switch the [user] context to that specific user. + * @param {string} externalId + * @param {string} jwtToken - Optional * @returns void */ - login(externalId: string): void { - window.cordova.exec(function () { }, function () { }, "OneSignalPush", "login", [externalId]); + login(externalId: string, jwtToken?: string): void { + // if no jwt token, pass null + const args = jwtToken ? [externalId, jwtToken] : [externalId]; + window.cordova.exec(function () { }, function () { }, "OneSignalPush", "login", args); } /** - * Logout the user previously logged in via [login]. The [user] property now references a new device-scoped user. - * @param {string} externalId + * Log out the user previously logged in via [login]. The [user] property now references a new device-scoped user. * @returns void */ logout(): void { window.cordova.exec(function () { }, function () { }, "OneSignalPush", "logout"); } + /** + * Update the JWT token for a user. + * @param {string} externalId + * @param {string} token + */ + updateUserJwt(externalId: string, token: string): void { + window.cordova.exec(function () { }, function () { }, "OneSignalPush", "updateUserJwt", [externalId, token]); + } + + /** + * Add a callback that fires when the user's JWT is invalidated. + * @param event + * @param listener + */ + addEventListener(event: "userJwtInvalidated", listener: (event: UserJwtInvalidatedEvent) => void) { + this._userJwtInvalidatedEventListenerList.push(listener as (event: UserJwtInvalidatedEvent) => void); + const userJwtInvalidatedCallBackProcessor = (event: UserJwtInvalidatedEvent) => { + this._processFunctionList(this._userJwtInvalidatedEventListenerList, event); + }; + window.cordova.exec(userJwtInvalidatedCallBackProcessor, function(){}, "OneSignalPush", "addUserJwtInvalidatedListener", []); + } + + /** + * Remove a UserJwtInvalidated Listener that has been previously added. + * @param event + * @param listener + */ + removeEventListener(event: "userJwtInvalidated", listener: (event: UserJwtInvalidatedEvent) => void) { + let index = this._userJwtInvalidatedEventListenerList.indexOf(listener); + if (index !== -1) { + this._userJwtInvalidatedEventListenerList.splice(index, 1); + } + } + /** * Determines whether a user must consent to privacy prior to their user data being sent up to OneSignal. This should be set to true prior to the invocation of initialization to ensure compliance. * @param {boolean} required