Skip to content

Instantly share code, notes, and snippets.

@shanemduggan
Last active September 21, 2021 01:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanemduggan/b22c60fa1d0bacf364c91e83efe0926c to your computer and use it in GitHub Desktop.
Save shanemduggan/b22c60fa1d0bacf364c91e83efe0926c to your computer and use it in GitHub Desktop.
Natively retrieve initial notification (iOS)
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;
import { NativeModules, Platform } from 'react-native';
async function getInitialNotification() {
if (Platform.OS === 'ios') {
return await NativeModules.NativeHelpers.getInitialNotification();
}
return null;
}
export default {
getInitialNotification
};
#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
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