Skip to content

Commit

Permalink
[iOS] Add getters for onesignal ID/external ID, add user state observer
Browse files Browse the repository at this point in the history
  • Loading branch information
jennantilla committed Jan 9, 2024
1 parent 525cf4f commit 4de73f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ios/OneSignalPush.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#import <OneSignalFramework/OneSignalFramework.h>

@interface OneSignalPush : CDVPlugin <OSNotificationPermissionObserver, OSNotificationLifecycleListener, OSNotificationClickListener, OSPushSubscriptionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener>
@interface OneSignalPush : CDVPlugin <OSNotificationPermissionObserver, OSNotificationLifecycleListener, OSNotificationClickListener, OSPushSubscriptionObserver, OSInAppMessageLifecycleListener, OSInAppMessageClickListener, OSUserStateObserver>

- (void)setProvidesNotificationSettingsView:(CDVInvokedUrlCommand* _Nonnull)command;
- (void)addForegroundLifecycleListener:(CDVInvokedUrlCommand* _Nonnull)command;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/ios/OneSignalPush.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4de73f3

Please sign in to comment.