From 7fb4de3c5bc4acb5e0f07f9523a6aafbac8a1237 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Thu, 30 Apr 2020 18:13:39 +0200 Subject: [PATCH] Augmented app delegate to use ios notifications --- ios/Campus/AppDelegate.h | 3 ++- ios/Campus/AppDelegate.m | 37 +++++++++++++++++++++++++++++++++++++ src/utils/Notifications.js | 4 ++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/ios/Campus/AppDelegate.h b/ios/Campus/AppDelegate.h index 224a590..0d6cd95 100644 --- a/ios/Campus/AppDelegate.h +++ b/ios/Campus/AppDelegate.h @@ -7,8 +7,9 @@ #import #import +#import -@interface AppDelegate : UIResponder +@interface AppDelegate : UIResponder @property (nonatomic, strong) UIWindow *window; diff --git a/ios/Campus/AppDelegate.m b/ios/Campus/AppDelegate.m index eda1798..55ba1e5 100644 --- a/ios/Campus/AppDelegate.m +++ b/ios/Campus/AppDelegate.m @@ -12,6 +12,8 @@ #import #import "RNSplashScreen.h" +#import +#import @implementation AppDelegate @@ -37,10 +39,45 @@ self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; +// Define UNUserNotificationCenter + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + center.delegate = self; [RNSplashScreen show]; return YES; } +//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); +} +// Required to register for notifications +- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings +{ + [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings]; +} +// Required for the register event. +- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken +{ + [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; +} +// 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 +{ + [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; +} +// Required for the registrationError event. +- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error +{ + [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; +} +// Required for the localNotification event. +- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification +{ + [RNCPushNotificationIOS didReceiveLocalNotification:notification]; +} + - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #ifdef DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; diff --git a/src/utils/Notifications.js b/src/utils/Notifications.js index 0126e8d..6203056 100644 --- a/src/utils/Notifications.js +++ b/src/utils/Notifications.js @@ -14,13 +14,13 @@ const reminderIdMultiplicator = 100; */ export async function askPermissions() { return new Promise(((resolve, reject) => { - checkNotifications().then(({status, settings}) => { + checkNotifications().then(({status}) => { if (status === RESULTS.GRANTED) resolve(); else if (status === RESULTS.BLOCKED) reject() else { - requestNotifications().then(({status, settings}) => { + requestNotifications().then(({status}) => { if (status === RESULTS.GRANTED) resolve(); else