Skip to content

Commit

Permalink
ios deps: Use PushNotificationIOS from react-native-community, not RN…
Browse files Browse the repository at this point in the history
… core.

In a recent commit, we asked RN's PushNotificationIOS to take
responsibilities that were previously done by
wix/react-native-notifications, and removed the Wix library.

Now, we account for the fact that PushNotificationsIOS from RN is
deprecated [1] and asks us to use
@react-native-community/push-notification-ios instead. So, do.

These were my steps:

1. Use the setup instructions for `PushNotificationIOS` [1] from RN
   v0.60 to tear it down.

2. Follow the setup instructions for
   @react-native-community/push-notification-ios at the latest,
   v1.2.2 [2]. The native code closely matches what was there
   before, which makes sense. There are a few additions, and notes
   on old iOS APIs.

3. Follow the simple "Migrating..." instructions [3] that say you
   just have to change the imports; no call site changes are
   necessary.

4. Change a few comments that refer to details of the directory
   structure or implementation of the library.

5. Test thoroughly, as in the previous commit, and observe the same
   results.

[1]: https://reactnative.dev/docs/0.60/pushnotificationios
[2]: https://github.com/react-native-community/push-notification-ios/blob/v1.2.2/README.md
[3]: https://github.com/react-native-community/push-notification-ios/blob/v1.2.2/README.md#migrating-from-the-core-react-native-module

Fixes: zulip#4115
  • Loading branch information
chrisbobbe committed Jun 17, 2020
1 parent 0670ab1 commit bbbf705
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 25 deletions.
3 changes: 0 additions & 3 deletions ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ target 'ZulipMobile' do
pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
# This is deprecated upstream; removing it is #4115. See
# https://reactnative.dev/docs/pushnotificationios.
pod 'React-RCTPushNotification', :path => '../node_modules/react-native/Libraries/PushNotificationIOS'
pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
Expand Down
14 changes: 7 additions & 7 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ PODS:
- React-Core (= 0.60.6)
- React-RCTNetwork (0.60.6):
- React-Core (= 0.60.6)
- React-RCTPushNotification (0.60.6):
- React-Core (= 0.60.6)
- React-RCTSettings (0.60.6):
- React-Core (= 0.60.6)
- React-RCTText (0.60.6):
Expand All @@ -117,6 +115,8 @@ PODS:
- React-Core
- RNCAsyncStorage (1.6.3):
- React
- RNCPushNotificationIOS (1.2.2):
- React
- RNDeviceInfo (0.21.5):
- React
- RNSentry (1.0.9):
Expand Down Expand Up @@ -181,13 +181,13 @@ DEPENDENCIES:
- React-RCTImage (from `../node_modules/react-native/Libraries/Image`)
- React-RCTLinking (from `../node_modules/react-native/Libraries/LinkingIOS`)
- React-RCTNetwork (from `../node_modules/react-native/Libraries/Network`)
- React-RCTPushNotification (from `../node_modules/react-native/Libraries/PushNotificationIOS`)
- React-RCTSettings (from `../node_modules/react-native/Libraries/Settings`)
- React-RCTText (from `../node_modules/react-native/Libraries/Text`)
- React-RCTVibration (from `../node_modules/react-native/Libraries/Vibration`)
- React-RCTWebSocket (from `../node_modules/react-native/Libraries/WebSocket`)
- rn-fetch-blob (from `../node_modules/rn-fetch-blob`)
- "RNCAsyncStorage (from `../node_modules/@react-native-community/async-storage`)"
- "RNCPushNotificationIOS (from `../node_modules/@react-native-community/push-notification-ios`)"
- RNDeviceInfo (from `../node_modules/react-native-device-info`)
- "RNSentry (from `../node_modules/@sentry/react-native`)"
- RNSound (from `../node_modules/react-native-sound`)
Expand Down Expand Up @@ -279,8 +279,6 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/Libraries/LinkingIOS"
React-RCTNetwork:
:path: "../node_modules/react-native/Libraries/Network"
React-RCTPushNotification:
:path: "../node_modules/react-native/Libraries/PushNotificationIOS"
React-RCTSettings:
:path: "../node_modules/react-native/Libraries/Settings"
React-RCTText:
Expand All @@ -293,6 +291,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/rn-fetch-blob"
RNCAsyncStorage:
:path: "../node_modules/@react-native-community/async-storage"
RNCPushNotificationIOS:
:path: "../node_modules/@react-native-community/push-notification-ios"
RNDeviceInfo:
:path: "../node_modules/react-native-device-info"
RNSentry:
Expand Down Expand Up @@ -372,13 +372,13 @@ SPEC CHECKSUMS:
React-RCTImage: 273501f0529775962551613259c20ccdf1a87cd2
React-RCTLinking: 76c88b3cc98657915a2ba2f20d208e44d0530a43
React-RCTNetwork: 77c11e672ccdcc33da5d047705f100b016497b15
React-RCTPushNotification: 8f11da5cd3dd7d15b8654268397637fec122210a
React-RCTSettings: f727c25ad26a8a9bd7272a8ba93781bd1f53952a
React-RCTText: d91537e29e38dc69cf09cbca0875cf5dc7402da6
React-RCTVibration: 7655d72dfb919dd6d8e135ca108a5a2bd9fcd7b4
React-RCTWebSocket: 7cd2c8d0f8ddd680dc76404defba7ab1f56b83af
rn-fetch-blob: f525a73a78df9ed5d35e67ea65e79d53c15255bc
RNCAsyncStorage: 3c304d1adfaea02ec732ac218801cb13897aa8c0
RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde
RNDeviceInfo: e7c5fcde13d40e161d8a27f6c5dc69c638936002
RNSentry: 2803ba8c8129dcf26b79e9b4d8c80168be6e4390
RNSound: da030221e6ac7e8290c6b43f2b5f2133a8e225b0
Expand All @@ -399,6 +399,6 @@ SPEC CHECKSUMS:
UMTaskManagerInterface: 1e70fe58b872355f0ecb44fb81bb1a16484047f0
yoga: 5079887aa3e4c62142d6bcee493022643ee4d730

PODFILE CHECKSUM: 21c8ba3e839e2d99477fac31255085d875c73084
PODFILE CHECKSUM: 65e3a24dd14288e63a9cc70e88254828c4344e5b

COCOAPODS: 1.9.1
1 change: 0 additions & 1 deletion ios/ZulipMobile-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
#import <React/RCTConvert.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTPushNotificationManager.h>
3 changes: 2 additions & 1 deletion ios/ZulipMobile/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
#import <UserNotifications/UNUserNotificationCenter.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
@property (nonatomic, strong) UIWindow *window;
Expand Down
25 changes: 17 additions & 8 deletions ios/ZulipMobile/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#import <asl.h>
#import <React/RCTLog.h>
#import <UserNotifications/UserNotifications.h>
#import <React/RCTPushNotificationManager.h>
#import <RNCPushNotificationIOS.h>
#import <UMCore/UMModuleRegistry.h>
#import <UMReactNativeAdapter/UMNativeModulesProxy.h>
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
Expand Down Expand Up @@ -83,34 +83,43 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}

// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}

// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

// Required for the localNotification event.
// iOS 10+ Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[RNCPushNotificationIOS didReceiveNotificationResponse:response];
completionHandler();
}

// iOS 4-10 Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RCTPushNotificationManager didReceiveLocalNotification:notification];
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

// Called when a notification is delivered to a foreground app.
// iOS 10.0+: Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const transformModulesWhitelist = [
// @rnc/async-storage itself is precompiled, but its mock-helper is not
'@react-native-community/async-storage',
'@react-native-community/cameraroll',
'@react-native-community/push-notification-ios',
'@expo/react-native-action-sheet',
'react-navigation',
'@sentry/react-native',
Expand Down
12 changes: 12 additions & 0 deletions jest/jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ jest.mock('Linking', () => ({
getInitialURL: jest.fn(),
}));

// Without this, we get lots of these errors on importing the module:
// `Invariant Violation: Native module cannot be null.`
jest.mock('@react-native-community/push-notification-ios', () => ({
presentLocalNotification: jest.fn(),
scheduleLocalNotification: jest.fn(),
cancelAllLocalNotifications: jest.fn(),
removeAllDeliveredNotifications: jest.fn(),
getDeliveredNotifications: jest.fn(),
removeDeliveredNotifications: jest.fn(),
// etc. (incomplete)
}));

jest.mock('rn-fetch-blob', () => ({
DocumentDir: () => {},
}));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@react-native-community/async-storage": "^1.6.3",
"@react-native-community/cameraroll": "^1.7.2",
"@react-native-community/netinfo": "^3.2.1",
"@react-native-community/push-notification-ios": "^1.2.2",
"@sentry/react-native": "^1.0.9",
"@unimodules/core": "~4.0.0",
"@zulip/shared": "^0.0.2",
Expand Down
11 changes: 6 additions & 5 deletions src/notification/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow strict-local */
import { DeviceEventEmitter, NativeModules, Platform, PushNotificationIOS } from 'react-native';
import type { PushNotificationEventName } from 'react-native/Libraries/PushNotificationIOS/PushNotificationIOS';
import { DeviceEventEmitter, NativeModules, Platform } from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import type { PushNotificationEventName } from '@react-native-community/push-notification-ios';

import type { Notification } from './types';
import type { Auth, Dispatch, Identity, Narrow, User } from '../types';
Expand Down Expand Up @@ -118,8 +119,8 @@ const getInitialNotification = async (): Promise<Notification | null> => {
}

// This is actually typed as ?Object (and so effectively `any`); but if
// present, it must be a JSONable dictionary. (See PushNotificationIOS.js and
// RCTPushNotificationManager.m in Libraries/PushNotificationIOS.)
// present, it must be a JSONable dictionary. (See js/index.js and
// ios/RNCPushNotificationIOS.m in @rnc-push-notification-ios.)
const data: ?JSONableDict = notification.getData();
if (!data) {
return null;
Expand Down Expand Up @@ -265,7 +266,7 @@ export class NotificationListener {
*/
export const getNotificationToken = () => {
if (Platform.OS === 'ios') {
// This leads to a call in RN's PushNotificationIOS to this:
// This leads to a call in RNC's PushNotificationIOS to this:
// https://developer.apple.com/documentation/uikit/uiapplication/1622932-registerusernotificationsettings
// (deprecated after iOS 10, yikes!); which after possibly prompting the
// user causes "the app" (i.e. the platform part) to call this:
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,13 @@
resolved "https://registry.yarnpkg.com/@react-native-community/netinfo/-/netinfo-3.2.1.tgz#cd073b81a4b978f7f55f1a960a0b56c462813e02"
integrity sha512-A2qANOnlRDVe+8kMbKMwy3/0bOlOA2+y8DyWg2Rv2KHICIfin+oxixbG0ewAOLQdLkSEyyumZXRmIVl7VI/KJg==

"@react-native-community/push-notification-ios@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@react-native-community/push-notification-ios/-/push-notification-ios-1.2.2.tgz#e4437a89bddca2cdeac96d17e02cde34336caaf3"
integrity sha512-0+DL68CgH5TF8WKwdjI9pul+mPKChGk9uHYcMJ0C6b296W5BQVaBQP6On2QYOgL/EWOWNWeBenW65FjKzWFf0g==
dependencies:
invariant "^2.2.4"

"@rpl/badge-up@2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@rpl/badge-up/-/badge-up-2.2.0.tgz#79a5ccf72bdb1777390bb7e4caa08dc42d57dd9a"
Expand Down

0 comments on commit bbbf705

Please sign in to comment.