From 70dd394cecad3890e2550cc006a4b9e622369d7b Mon Sep 17 00:00:00 2001 From: philip lisitza Date: Tue, 25 Feb 2020 09:18:50 -0600 Subject: [PATCH] fix: updated to reflect need for UNUserNotificationCenter. also brought in line with linter recommendations --- README.md | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4074d8613..a63262bf0 100644 --- a/README.md +++ b/README.md @@ -11,28 +11,33 @@ React Native Push Notification API for iOS. ### Install -``` +```bash yarn add @react-native-community/push-notification-ios ``` ### Link + There are a couple of cases for linking. Choose the appropriate one. + - `react-native >= 0.60` The package is [automatically linked](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md) when building the app. All you need to do is: -``` + +```bash cd ios && pod install ``` - `react-native <= 0.59` -``` + +```bash react-native link @react-native-community/push-notification-ios ``` - upgrading to `react-native >= 0.60` First, unlink the library. Then follow the instructions above. - ``` + + ```bash react-native unlink @react-native-community/push-notification-ios ``` @@ -45,10 +50,13 @@ react-native link @react-native-community/push-notification-ios Finally, to enable support for `notification` and `register` events you need to augment your AppDelegate. At the top of the file: -``` + +```objective-c #import ``` + Then, add the following lines: + ```objective-c // Required to register for notifications - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings @@ -78,7 +86,32 @@ fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler } ``` +Also, if not already present, at the top of the file: + +```objective-c +#import +``` + +Inside didFinishLaunchingWithOptions or equivalent: + +```objective-c + // Define UNUserNotificationCenter + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + center.delegate = self; +``` + +And at the bottom (before @end): + +```objective-c + // Called when a notification is delivered to a foreground app. + -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler + { + completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); + } +``` + ## Migrating from the core `react-native` module + This module was created when the PushNotificationIOS was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from: ```js @@ -98,4 +131,4 @@ import PushNotificationIOS from "@react-native-community/push-notification-ios"; [license-badge]: https://img.shields.io/npm/l/@react-native-community/push-notification-ios.svg?style=flat-square [license]: https://opensource.org/licenses/MIT [lean-core-badge]: https://img.shields.io/badge/Lean%20Core-Extracted-brightgreen.svg?style=flat-square -[lean-core-issue]: https://github.com/facebook/react-native/issues/23313 \ No newline at end of file +[lean-core-issue]: https://github.com/facebook/react-native/issues/23313