Skip to content

Commit

Permalink
ios notifications: Update the badge count if the app is foregrounded.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
chrisbobbe committed May 10, 2021
1 parent eda22d0 commit c6383df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ios/ZulipMobile/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,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
17 changes: 17 additions & 0 deletions ios/ZulipMobile/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

return YES;
}

Expand Down Expand Up @@ -120,4 +125,16 @@ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UI
[RNNotifications didReceiveLocalNotification:notification];
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}

// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
// Update the badge count. Do not play sound or show an alert. For
// these options see
// https://developer.apple.com/documentation/usernotifications/unnotificationpresentationoptions?language=objc
completionHandler(UNNotificationPresentationOptionBadge);
}

@end

0 comments on commit c6383df

Please sign in to comment.