Browse Source

Augmented app delegate to use ios notifications

Arnaud Vergnet 1 year ago
parent
commit
7fb4de3c5b
3 changed files with 41 additions and 3 deletions
  1. 2
    1
      ios/Campus/AppDelegate.h
  2. 37
    0
      ios/Campus/AppDelegate.m
  3. 2
    2
      src/utils/Notifications.js

+ 2
- 1
ios/Campus/AppDelegate.h View File

@@ -7,8 +7,9 @@
7 7
 
8 8
 #import <UIKit/UIKit.h>
9 9
 #import <React/RCTBridgeDelegate.h>
10
+#import <UserNotifications/UNUserNotificationCenter.h>
10 11
 
11
-@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
12
+@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
12 13
 
13 14
 @property (nonatomic, strong) UIWindow *window;
14 15
 

+ 37
- 0
ios/Campus/AppDelegate.m View File

@@ -12,6 +12,8 @@
12 12
 
13 13
 #import <React/RCTLinkingManager.h>
14 14
 #import "RNSplashScreen.h"
15
+#import <RNCPushNotificationIOS.h>
16
+#import <UserNotifications/UserNotifications.h>
15 17
 
16 18
 @implementation AppDelegate
17 19
 
@@ -37,10 +39,45 @@
37 39
   self.window.rootViewController = rootViewController;
38 40
   [self.window makeKeyAndVisible];
39 41
 
42
+// Define UNUserNotificationCenter
43
+  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
44
+  center.delegate = self;
40 45
   [RNSplashScreen show];
41 46
   return YES;
42 47
 }
43 48
 
49
+//Called when a notification is delivered to a foreground app.
50
+-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
51
+{
52
+  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
53
+}
54
+// Required to register for notifications
55
+- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
56
+{
57
+ [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
58
+}
59
+// Required for the register event.
60
+- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
61
+{
62
+ [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
63
+}
64
+// Required for the notification event. You must call the completion handler after handling the remote notification.
65
+- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
66
+fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
67
+{
68
+  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
69
+}
70
+// Required for the registrationError event.
71
+- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
72
+{
73
+ [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
74
+}
75
+// Required for the localNotification event.
76
+- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
77
+{
78
+ [RNCPushNotificationIOS didReceiveLocalNotification:notification];
79
+}
80
+
44 81
 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
45 82
 #ifdef DEBUG
46 83
   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

+ 2
- 2
src/utils/Notifications.js View File

@@ -14,13 +14,13 @@ const reminderIdMultiplicator = 100;
14 14
  */
15 15
 export async function askPermissions() {
16 16
     return new Promise(((resolve, reject) => {
17
-        checkNotifications().then(({status, settings}) => {
17
+        checkNotifications().then(({status}) => {
18 18
             if (status === RESULTS.GRANTED)
19 19
                 resolve();
20 20
             else if (status === RESULTS.BLOCKED)
21 21
                 reject()
22 22
             else {
23
-                requestNotifications().then(({status, settings}) => {
23
+                requestNotifications().then(({status}) => {
24 24
                     if (status === RESULTS.GRANTED)
25 25
                         resolve();
26 26
                     else

Loading…
Cancel
Save