Skip to content

Commit 7a622ac

Browse files
committed
ios notifications: Update the badge count if the app is foregrounded.
This is based on the recommended setup for PushNotificationIOS from React Native [1], so we might have included it in the next commit, where we fully set that up. But as Greg points out [2], this code is purely using upstream iOS APIs, so it doesn't depend on whether we're using that RN module or something else, so it's fine to do here on its own. In a departure from those instructions, we decide to just pass the thing (`UNNotificationPresentationOptionBadge`) that updates the badge count when the app is foregrounded. This is a necessary part of zulip#4182, but one thing we'll still need to do for that is test that the server is sending the right badge counts to the client. [1] https://reactnative.dev/docs/0.62/pushnotificationios. These instructions are actually missing a required change to the AppDelegate.h, which we take here, having seen it in the instructions for @react-native-community/push-notification-ios (at https://github.com/react-native-push-notification-ios/push-notification-ios#update-appdelegateh). [2] zulip#4163 (comment)
1 parent bb7afe9 commit 7a622ac

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ios/ZulipMobile/AppDelegate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#import <React/RCTBridgeDelegate.h>
22
#import <UIKit/UIKit.h>
33
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
4+
#import <UserNotifications/UNUserNotificationCenter.h>
45

5-
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
6+
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
67

78
@property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
89
@property (nonatomic, strong) UIWindow *window;

ios/ZulipMobile/AppDelegate.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5757
rootViewController.view = rootView;
5858
self.window.rootViewController = rootViewController;
5959
[self.window makeKeyAndVisible];
60+
61+
// Define UNUserNotificationCenter
62+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
63+
center.delegate = self;
64+
6065
return YES;
6166
}
6267

@@ -120,4 +125,16 @@ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UI
120125
[RNNotifications didReceiveLocalNotification:notification];
121126
[RCTPushNotificationManager didReceiveLocalNotification:notification];
122127
}
128+
129+
// Called when a notification is delivered to a foreground app.
130+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
131+
willPresentNotification:(UNNotification *)notification
132+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
133+
{
134+
// Update the badge count. Do not play sound or show an alert. For
135+
// these options see
136+
// https://developer.apple.com/documentation/usernotifications/unnotificationpresentationoptions?language=objc
137+
completionHandler(UNNotificationPresentationOptionBadge);
138+
}
139+
123140
@end

0 commit comments

Comments
 (0)