Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getInitialNotification() on iOS #626

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -478,4 +478,15 @@ export default class OneSignal {
}
}

static getInitialNotification() {
if (!checkIfInitialized()) return;

//returns a promise
if (Platform.OS === 'android') {
console.log("This function is not supported on Android");
return
}
return RNOneSignal.getInitialNotification();
}

}
4 changes: 2 additions & 2 deletions ios/RCTOneSignal/RCTOneSignal.m
Expand Up @@ -76,8 +76,8 @@ - (void)didBeginObserving {

dispatch_async(dispatch_get_main_queue(), ^{
if (coldStartOSNotificationOpenedResult) {
[self handleRemoteNotificationOpened:[coldStartOSNotificationOpenedResult stringify]];
coldStartOSNotificationOpenedResult = nil;
NSDictionary *json = [self jsonObjectWithString:[coldStartOSNotificationOpenedResult stringify]];
[RCTOneSignalEventEmitter setInitialNotification:json];
}
});
}
Expand Down
1 change: 1 addition & 0 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.h
Expand Up @@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
@interface RCTOneSignalEventEmitter : RCTEventEmitter <RCTBridgeModule>

+ (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body;
+ (void)setInitialNotification:(NSDictionary *)body;
+ (BOOL)hasSetBridge;

@end
22 changes: 22 additions & 0 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Expand Up @@ -33,6 +33,8 @@ +(BOOL)requiresMainQueueSetup {

RCT_EXPORT_MODULE(RCTOneSignal)

NSDictionary* initialNotification;

#pragma mark RCTEventEmitter Subclass Methods

-(instancetype)init {
Expand Down Expand Up @@ -85,6 +87,9 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:body];
}

+ (void)setInitialNotification:(NSDictionary *)body {
initialNotification = body;
}

#pragma mark Exported Methods

Expand Down Expand Up @@ -350,6 +355,23 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal setExternalUserId:externalId];
}

RCT_REMAP_METHOD(getInitialNotification,
getInitialNotificationWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (initialNotification) {
resolve(initialNotification);
} else {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"No initial notification", nil),
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"Initial notification not found", nil)
};
NSError *error = [NSError errorWithDomain:@"RCTOneSignal" code:1 userInfo:userInfo];
reject(@"no_initial_notification", @"There was no notification on app opening", error);
}
}


RCT_EXPORT_METHOD(removeExternalUserId) {
[OneSignal removeExternalUserId];
}
Expand Down