Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- */
- #import "AppDelegate.h"
- #import <React/RCTBridge.h>
- #import <React/RCTBundleURLProvider.h>
- #import <React/RCTRootView.h>
- #import "RNCallKeep.h"
- #import <UserNotifications/UserNotifications.h>
- #import <RNCPushNotificationIOS.h>
- #import <PushKit/PushKit.h> /* <------ add this line */
- #import "RNVoipPushNotificationManager.h" /* <------ add this line */
- @implementation AppDelegate
- // 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 localNotification event
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- didReceiveNotificationResponse:(UNNotificationResponse *)response
- withCompletionHandler:(void (^)(void))completionHandler
- {
- [RNCPushNotificationIOS didReceiveNotificationResponse:response];
- }
- //
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
- [RNVoipPushNotificationManager voipRegistration];
- RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
- moduleName:@"euclid"
- initialProperties:nil];
- rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
- self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- UIViewController *rootViewController = [UIViewController new];
- rootViewController.view = rootView;
- self.window.rootViewController = rootViewController;
- [self.window makeKeyAndVisible];
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
- center.delegate = self;
- return YES;
- }
- -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
- {
- completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
- }
- - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
- {
- #if DEBUG
- return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
- #else
- return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
- #endif
- }
- - (BOOL)application:(UIApplication *)application
- continueUserActivity:(NSUserActivity *)userActivity
- restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler
- {
- return [RNCallKeep application:application
- continueUserActivity:userActivity
- restorationHandler:restorationHandler];
- }
- /* Add PushKit delegate method */
- // --- Handle updated push credentials
- - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type {
- // Register VoIP push token (a property of PKPushCredentials) with server
- [RNVoipPushNotificationManager didUpdatePushCredentials:credentials forType:(NSString *)type];
- }
- - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type
- {
- // --- The system calls this method when a previously provided push token is no longer valid for use. No action is necessary on your part to reregister the push type. Instead, use this method to notify your server not to send push notifications using the matching push token.
- }
- // --- Handle incoming pushes (for ios <= 10)
- - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type {
- [RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type];
- }
- // --- Handle incoming pushes (for ios >= 11)
- - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion {
- // --- NOTE: apple forced us to invoke callkit ASAP when we receive voip push
- // --- see: react-native-callkeep
- // --- Retrieve information from your voip push payload
- NSString *uuid = payload.dictionaryPayload[@"uuid"];
- NSString *callerName = [NSString stringWithFormat:@"%@ (Connecting...)", payload.dictionaryPayload[@"callerName"]];
- NSString *handle = payload.dictionaryPayload[@"handle"];
- // --- this is optional, only required if you want to call `completion()` on the js side
- [RNVoipPushNotificationManager addCompletionHandler:uuid completionHandler:completion];
- // --- Process the received push
- [RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type];
- // --- You should make sure to report to callkit BEFORE execute `completion()`
- // RNCallKeep >4.0.0
- [RNCallKeep reportNewIncomingCall: uuid
- handle: handle
- handleType: @"generic"
- hasVideo: YES
- localizedCallerName: callerName
- supportsHolding: YES
- supportsDTMF: YES
- supportsGrouping: YES
- supportsUngrouping: YES
- fromPushKit: YES
- payload: payload.dictionaryPayload
- withCompletionHandler: nil];
- completion();
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement