Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright (c) Facebook, Inc. and its affiliates.
  3.  *
  4.  * This source code is licensed under the MIT license found in the
  5.  * LICENSE file in the root directory of this source tree.
  6.  */
  7.  
  8. #import "AppDelegate.h"
  9.  
  10. #import <React/RCTBridge.h>
  11. #import <React/RCTBundleURLProvider.h>
  12. #import <React/RCTRootView.h>
  13.  
  14. #import "RNCallKeep.h"
  15.  
  16. #import <UserNotifications/UserNotifications.h>
  17. #import <RNCPushNotificationIOS.h>
  18.  
  19. #import <PushKit/PushKit.h>                    /* <------ add this line */
  20. #import "RNVoipPushNotificationManager.h"      /* <------ add this line */
  21.  
  22. @implementation AppDelegate
  23.  
  24. // Required for the register event.
  25. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  26. {
  27.  [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  28. }
  29. // Required for the notification event. You must call the completion handler after handling the remote notification.
  30. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  31. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  32. {
  33.   [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
  34. }
  35. // Required for the registrationError event.
  36. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  37. {
  38.  [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
  39. }
  40. // Required for localNotification event
  41. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  42. didReceiveNotificationResponse:(UNNotificationResponse *)response
  43.          withCompletionHandler:(void (^)(void))completionHandler
  44. {
  45.   [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  46. }
  47.  
  48. //
  49.  
  50. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  51. {
  52.   RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  53.  
  54.   [RNVoipPushNotificationManager voipRegistration];
  55.  
  56.   RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
  57.                                                    moduleName:@"euclid"
  58.                                             initialProperties:nil];
  59.  
  60.   rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  61.  
  62.   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  63.   UIViewController *rootViewController = [UIViewController new];
  64.   rootViewController.view = rootView;
  65.   self.window.rootViewController = rootViewController;
  66.   [self.window makeKeyAndVisible];
  67.   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  68.   center.delegate = self;
  69.  
  70.   return YES;
  71. }
  72.  
  73. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  74. {
  75.   completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  76. }
  77.  
  78. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  79. {
  80. #if DEBUG
  81.   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  82. #else
  83.   return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  84. #endif
  85. }
  86.  
  87. - (BOOL)application:(UIApplication *)application
  88. continueUserActivity:(NSUserActivity *)userActivity
  89.   restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler
  90. {
  91.   return [RNCallKeep application:application
  92.            continueUserActivity:userActivity
  93.              restorationHandler:restorationHandler];
  94. }
  95.  
  96. /* Add PushKit delegate method */
  97.  
  98. // --- Handle updated push credentials
  99. - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type {
  100.   // Register VoIP push token (a property of PKPushCredentials) with server
  101.   [RNVoipPushNotificationManager didUpdatePushCredentials:credentials forType:(NSString *)type];
  102. }
  103.  
  104. - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type
  105. {
  106.   // --- 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.
  107. }
  108.  
  109. // --- Handle incoming pushes (for ios <= 10)
  110. - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type {
  111.   [RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type];
  112. }
  113.  
  114. // --- Handle incoming pushes (for ios >= 11)
  115. - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion {
  116.   // --- NOTE: apple forced us to invoke callkit ASAP when we receive voip push
  117.   // --- see: react-native-callkeep
  118.  
  119.   // --- Retrieve information from your voip push payload
  120.   NSString *uuid = payload.dictionaryPayload[@"uuid"];
  121.   NSString *callerName = [NSString stringWithFormat:@"%@ (Connecting...)", payload.dictionaryPayload[@"callerName"]];
  122.   NSString *handle = payload.dictionaryPayload[@"handle"];
  123.  
  124.   // --- this is optional, only required if you want to call `completion()` on the js side
  125.   [RNVoipPushNotificationManager addCompletionHandler:uuid completionHandler:completion];
  126.  
  127.   // --- Process the received push
  128.   [RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type];
  129.  
  130.   // --- You should make sure to report to callkit BEFORE execute `completion()`
  131.   // RNCallKeep >4.0.0
  132.   [RNCallKeep reportNewIncomingCall: uuid
  133.                              handle: handle
  134.                          handleType: @"generic"
  135.                            hasVideo: YES
  136.                 localizedCallerName: callerName
  137.                     supportsHolding: YES
  138.                        supportsDTMF: YES
  139.                    supportsGrouping: YES
  140.                  supportsUngrouping: YES
  141.                         fromPushKit: YES
  142.                             payload: payload.dictionaryPayload
  143.               withCompletionHandler: nil];
  144.  
  145.   completion();
  146. }
  147.  
  148. @end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement