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 10, 2018
1 parent 08ef9f7 commit 45f9e44
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -355,4 +355,15 @@ export default class OneSignal {
return RNOneSignal.userProvidedPrivacyConsent();
}

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
21 changes: 21 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 @@ -343,5 +348,21 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal setLogLevel:logLevel visualLevel:visualLogLevel];
}

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);
}
}


@end

0 comments on commit 45f9e44

Please sign in to comment.