Augmented app delegate to use ios notifications

This commit is contained in:
Arnaud Vergnet 2020-04-30 18:13:39 +02:00
parent 9882873861
commit 7fb4de3c5b
3 changed files with 41 additions and 3 deletions

View file

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

View file

@ -12,6 +12,8 @@
#import <React/RCTLinkingManager.h> #import <React/RCTLinkingManager.h>
#import "RNSplashScreen.h" #import "RNSplashScreen.h"
#import <RNCPushNotificationIOS.h>
#import <UserNotifications/UserNotifications.h>
@implementation AppDelegate @implementation AppDelegate
@ -37,10 +39,45 @@
self.window.rootViewController = rootViewController; self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];
// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[RNSplashScreen show]; [RNSplashScreen show];
return YES; 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 { - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
#ifdef DEBUG #ifdef DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

View file

@ -14,13 +14,13 @@ const reminderIdMultiplicator = 100;
*/ */
export async function askPermissions() { export async function askPermissions() {
return new Promise(((resolve, reject) => { return new Promise(((resolve, reject) => {
checkNotifications().then(({status, settings}) => { checkNotifications().then(({status}) => {
if (status === RESULTS.GRANTED) if (status === RESULTS.GRANTED)
resolve(); resolve();
else if (status === RESULTS.BLOCKED) else if (status === RESULTS.BLOCKED)
reject() reject()
else { else {
requestNotifications().then(({status, settings}) => { requestNotifications().then(({status}) => {
if (status === RESULTS.GRANTED) if (status === RESULTS.GRANTED)
resolve(); resolve();
else else