Skip to content

Commit 45f9e44

Browse files
committed
Add getInitialNotification() on iOS
1 parent 08ef9f7 commit 45f9e44

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,15 @@ export default class OneSignal {
355355
return RNOneSignal.userProvidedPrivacyConsent();
356356
}
357357

358+
static getInitialNotification() {
359+
if (!checkIfInitialized()) return;
360+
361+
//returns a promise
362+
if (Platform.OS === 'android') {
363+
console.log("This function is not supported on Android");
364+
return
365+
}
366+
return RNOneSignal.getInitialNotification();
367+
}
368+
358369
}

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: 21 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

@@ -343,5 +348,21 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
343348
[OneSignal setLogLevel:logLevel visualLevel:visualLogLevel];
344349
}
345350

351+
RCT_REMAP_METHOD(getInitialNotification,
352+
getInitialNotificationWithResolver:(RCTPromiseResolveBlock)resolve
353+
rejecter:(RCTPromiseRejectBlock)reject)
354+
{
355+
if (initialNotification) {
356+
resolve(initialNotification);
357+
} else {
358+
NSDictionary *userInfo = @{
359+
NSLocalizedDescriptionKey: NSLocalizedString(@"No initial notification", nil),
360+
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"Initial notification not found", nil)
361+
};
362+
NSError *error = [NSError errorWithDomain:@"RCTOneSignal" code:1 userInfo:userInfo];
363+
reject(@"no_initial_notification", @"There was no notification on app opening", error);
364+
}
365+
}
366+
346367

347368
@end

0 commit comments

Comments
 (0)