Browse Source

Augmented app delegate to use ios notifications

Arnaud Vergnet 4 years 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
 
7
 
8
 #import <UIKit/UIKit.h>
8
 #import <UIKit/UIKit.h>
9
 #import <React/RCTBridgeDelegate.h>
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
 @property (nonatomic, strong) UIWindow *window;
14
 @property (nonatomic, strong) UIWindow *window;
14
 
15
 

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

12
 
12
 
13
 #import <React/RCTLinkingManager.h>
13
 #import <React/RCTLinkingManager.h>
14
 #import "RNSplashScreen.h"
14
 #import "RNSplashScreen.h"
15
+#import <RNCPushNotificationIOS.h>
16
+#import <UserNotifications/UserNotifications.h>
15
 
17
 
16
 @implementation AppDelegate
18
 @implementation AppDelegate
17
 
19
 
37
   self.window.rootViewController = rootViewController;
39
   self.window.rootViewController = rootViewController;
38
   [self.window makeKeyAndVisible];
40
   [self.window makeKeyAndVisible];
39
 
41
 
42
+// Define UNUserNotificationCenter
43
+  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
44
+  center.delegate = self;
40
   [RNSplashScreen show];
45
   [RNSplashScreen show];
41
   return YES;
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
 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
81
 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
45
 #ifdef DEBUG
82
 #ifdef DEBUG
46
   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
83
   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

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

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

Loading…
Cancel
Save