Last active
September 21, 2021 01:42
-
-
Save shanemduggan/b22c60fa1d0bacf364c91e83efe0926c to your computer and use it in GitHub Desktop.
Natively retrieve initial notification (iOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { View } from 'react-native'; | |
import NativeHelpers from './NativeHelpers'; | |
class App extends Component { | |
componentDidMount() { | |
const initialPush = await NativeHelpers.getInitialNotification(); | |
// Do something with initial push | |
} | |
render() { | |
return ( | |
<View></View> | |
); | |
} | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NativeModules, Platform } from 'react-native'; | |
async function getInitialNotification() { | |
if (Platform.OS === 'ios') { | |
return await NativeModules.NativeHelpers.getInitialNotification(); | |
} | |
return null; | |
} | |
export default { | |
getInitialNotification | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <React/RCTBridgeModule.h> | |
@interface RCT_EXTERN_MODULE(NativeHelpers, NSObject) | |
RCT_EXTERN_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) | |
- (dispatch_queue_t)methodQueue | |
{ | |
return dispatch_get_main_queue(); | |
} | |
+ (BOOL)requiresMainQueueSetup | |
{ | |
return YES; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@objc(NativeHelpers) | |
class NativeHelpers: NSObject { | |
@objc | |
func getInitialNotification(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | |
let launchOptions = (UIApplication.shared.delegate as! AppDelegate).launchOptions | |
let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] | |
resolve(notification) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment