Skip to content

Commit 47c7d23

Browse files
committed
Add getInitialNotification() on iOS
1 parent 22f997d commit 47c7d23

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,15 @@ export default class OneSignal {
478478
}
479479
}
480480

481+
static getInitialNotification() {
482+
if (!checkIfInitialized()) return;
483+
484+
//returns a promise
485+
if (Platform.OS === 'android') {
486+
console.log("This function is not supported on Android");
487+
return
488+
}
489+
return RNOneSignal.getInitialNotification();
490+
}
491+
481492
}

ios/RCTOneSignal/RCTOneSignal.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ - (void)didBeginObserving {
7676

7777
dispatch_async(dispatch_get_main_queue(), ^{
7878
if (coldStartOSNotificationOpenedResult) {
79-
[self handleRemoteNotificationOpened:[coldStartOSNotificationOpenedResult stringify]];
80-
coldStartOSNotificationOpenedResult = nil;
79+
NSDictionary *json = [self jsonObjectWithString:[coldStartOSNotificationOpenedResult stringify]];
80+
[RCTOneSignalEventEmitter setInitialNotification:json];
8181
}
8282
});
8383
}

ios/RCTOneSignal/RCTOneSignalEventEmitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, OSNotificationEventTypes) {
2626
@interface RCTOneSignalEventEmitter : RCTEventEmitter <RCTBridgeModule>
2727

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

3132
@end

ios/RCTOneSignal/RCTOneSignalEventEmitter.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ +(BOOL)requiresMainQueueSetup {
3333

3434
RCT_EXPORT_MODULE(RCTOneSignal)
3535

36+
NSDictionary* initialNotification;
37+
3638
#pragma mark RCTEventEmitter Subclass Methods
3739

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

90+
+ (void)setInitialNotification:(NSDictionary *)body {
91+
initialNotification = body;
92+
}
8893

8994
#pragma mark Exported Methods
9095

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

358+
RCT_REMAP_METHOD(getInitialNotification,
359+
getInitialNotificationWithResolver:(RCTPromiseResolveBlock)resolve
360+
rejecter:(RCTPromiseRejectBlock)reject)
361+
{
362+
if (initialNotification) {
363+
resolve(initialNotification);
364+
} else {
365+
NSDictionary *userInfo = @{
366+
NSLocalizedDescriptionKey: NSLocalizedString(@"No initial notification", nil),
367+
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"Initial notification not found", nil)
368+
};
369+
NSError *error = [NSError errorWithDomain:@"RCTOneSignal" code:1 userInfo:userInfo];
370+
reject(@"no_initial_notification", @"There was no notification on app opening", error);
371+
}
372+
}
373+
374+
353375
RCT_EXPORT_METHOD(removeExternalUserId) {
354376
[OneSignal removeExternalUserId];
355377
}

0 commit comments

Comments
 (0)