From 4de73f381218bfadef8ea47bef81988ac772ab44 Mon Sep 17 00:00:00 2001 From: Jenna Antilla <46546946+jennantilla@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:26:09 -0800 Subject: [PATCH] [iOS] Add getters for onesignal ID/external ID, add user state observer --- src/ios/OneSignalPush.h | 7 +++++- src/ios/OneSignalPush.m | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/ios/OneSignalPush.h b/src/ios/OneSignalPush.h index e8308b73..7782cb2e 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; @@ -53,6 +53,10 @@ - (void)removeTags:(CDVInvokedUrlCommand* _Nonnull)command; - (void)getTags:(CDVInvokedUrlCommand* _Nonnull)command; +- (void)addUserStateObserver:(CDVInvokedUrlCommand* _Nonnull)command; +- (void)getOnesignalId:(CDVInvokedUrlCommand* _Nonnull)command; +- (void)getExternalId:(CDVInvokedUrlCommand* _Nonnull)command; + // Push Subscription - (void)addPushSubscriptionObserver:(CDVInvokedUrlCommand* _Nonnull)command; - (void)getPushSubscriptionId:(CDVInvokedUrlCommand* _Nonnull)command; @@ -69,6 +73,7 @@ - (void)canRequestPermission:(CDVInvokedUrlCommand* _Nonnull)command; - (void)registerForProvisionalAuthorization:(CDVInvokedUrlCommand* _Nonnull)command; - (void)clearAllNotifications:(CDVInvokedUrlCommand* _Nonnull)command; + // Android Only - Notifications - (void)removeNotification:(CDVInvokedUrlCommand* _Nonnull)command; - (void)removeGroupedNotifications:(CDVInvokedUrlCommand* _Nonnull)command; diff --git a/src/ios/OneSignalPush.m b/src/ios/OneSignalPush.m index 0b431f18..2e1a3562 100644 --- a/src/ios/OneSignalPush.m +++ b/src/ios/OneSignalPush.m @@ -165,6 +165,45 @@ - (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState *)s successCallback(subscriptionObserverCallbackId, [state jsonRepresentation]); } +- (void)onUserStateDidChangeWithState:(OSUserChangedState * _Nonnull)state { + NSString *onesignalId = state.current.onesignalId; + NSString *externalId = state.current.externalId; + + NSMutableDictionary *result = [NSMutableDictionary new]; + + NSMutableDictionary *currentObject = [NSMutableDictionary new]; + if (onesignalId.length > 0) { + currentObject[@"onesignalId"] = onesignalId; + } + if (externalId.length > 0) { + currentObject[@"externalId"] = externalId; + } + + result[@"current"] = currentObject; + + successCallback(userObserverCallbackId, result); +} + +- (void)getOnesignalId:(CDVInvokedUrlCommand *)command { + NSString *onesignalId = OneSignal.User.onesignalId; + + NSDictionary *result = @{ + @"value" : (onesignalId ? onesignalId : [NSNull null]) + }; + + successCallback(command.callbackId, result); +} + +- (void)getExternalId:(CDVInvokedUrlCommand *)command { + NSString *externalId = OneSignal.User.externalId; + + NSDictionary *result = @{ + @"value" : (externalId ? externalId : [NSNull null]) + }; + + successCallback(command.callbackId, result); +} + - (void)setProvidesNotificationSettingsView:(CDVInvokedUrlCommand *)command { BOOL providesView = command.arguments[0]; [OneSignal setProvidesNotificationSettingsView:providesView]; @@ -273,6 +312,14 @@ - (void)addPushSubscriptionObserver:(CDVInvokedUrlCommand*)command { [OneSignal.User.pushSubscription addObserver:self]; } +- (void)addUserStateObserver:(CDVInvokedUrlCommand*)command { + bool first = userObserverCallbackId == nil; + userObserverCallbackId = command.callbackId; + if (first) { + [OneSignal.User addObserver:self]; + } +} + - (void)getPushSubscriptionId:(CDVInvokedUrlCommand*)command { NSString *pushId = OneSignal.User.pushSubscription.id; if (pushId) {