Skip to content

Commit

Permalink
Add getInitialNotification() on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
habovh committed Sep 30, 2019
1 parent a32aa80 commit cfb46ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -458,4 +458,15 @@ export default class OneSignal {
RNOneSignal.pauseInAppMessages(pause);
}

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

0 comments on commit cfb46ae

Please sign in to comment.