Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. #import "AppDelegate+FirebasePlugin.h"
  2. #import "FirebasePlugin.h"
  3. #import "Firebase.h"
  4. #import <IBMMobileFirstPlatformFoundationHybrid/IBMMobileFirstPlatformFoundationHybrid.h>
  5. #import <IBMMobileFirstPlatformFoundation/WLAnalytics.h>
  6. #import <objc/runtime.h>
  7. #import "IonicDeeplinkPlugin.h"
  8. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  9. @import UserNotifications;
  10.  
  11. // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
  12. // running iOS 10 and above. Implement FIRMessagingDelegate to receive data message via FCM for
  13. // devices running iOS 10 and above.
  14. @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
  15. @end
  16.  
  17. #endif
  18.  
  19. #define kApplicationInBackgroundKey @"applicationInBackground"
  20. #define kDelegateKey @"delegate"
  21.  
  22. @implementation AppDelegate (FirebasePlugin)
  23.  
  24. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  25.  
  26. - (void)setDelegate:(id)delegate {
  27. objc_setAssociatedObject(self, kDelegateKey, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  28. }
  29.  
  30. - (id)delegate {
  31. return objc_getAssociatedObject(self, kDelegateKey);
  32. }
  33.  
  34. #endif
  35.  
  36. + (void)load {
  37. Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
  38. Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
  39. method_exchangeImplementations(original, swizzled);
  40. }
  41.  
  42. - (void)setApplicationInBackground:(NSNumber *)applicationInBackground {
  43. objc_setAssociatedObject(self, kApplicationInBackgroundKey, applicationInBackground, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  44. }
  45.  
  46. - (NSNumber *)applicationInBackground {
  47. return objc_getAssociatedObject(self, kApplicationInBackgroundKey);
  48. }
  49.  
  50. - (void)callDeepLink:(NSURL *) url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  51. IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
  52. if(plugin == nil) {
  53. return;
  54. }
  55. BOOL handled = [plugin handleLink: url];
  56. if(!handled) {
  57. // Pass event through to Cordova
  58. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object: url]];
  59.  
  60. // Send notice to the rest of our plugin that we didn't handle this URL
  61. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"IonicLinksUnhandledURL" object:[url absoluteString]]];
  62. }
  63. }
  64.  
  65. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  66. NSMutableString *sourceApp = [[NSMutableString alloc] init];
  67. NSMutableString *annotation = [[NSMutableString alloc] init];
  68. if([options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey]) {
  69. sourceApp = [options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey];
  70. }
  71.  
  72. if([options objectForKey:UIApplicationOpenURLOptionsAnnotationKey]) {
  73. annotation = [options objectForKey:UIApplicationOpenURLOptionsAnnotationKey];
  74. }
  75.  
  76. return [self application:app openURL:url sourceApplication:sourceApp annotation:annotation];
  77. }
  78.  
  79. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  80. IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
  81.  
  82. if(![self checkWhiteList:sourceApplication]) {
  83. NSLog(@"NOT IN WHITELIST");
  84. return NO;
  85. }
  86.  
  87. if(plugin == nil) {
  88. NSLog(@"plugin is null");
  89. __block id observer = nil;
  90. observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"CDVViewWillAppearNotification" object:nil queue:nil usingBlock: ^(NSNotification *block){
  91. if(observer != nil){
  92. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  93. observer = nil;
  94. }
  95. [self callDeepLink:url sourceApplication:sourceApplication annotation:annotation];
  96. }];
  97. }
  98.  
  99. BOOL handled = [plugin handleLink:url];
  100.  
  101. if(!handled) {
  102. // Pass event through to Cordova
  103. NSMutableDictionary * openURLData = [[NSMutableDictionary alloc] init];
  104.  
  105. [openURLData setValue:url forKey:@"url"];
  106.  
  107. if (sourceApplication) {
  108. [openURLData setValue:sourceApplication forKey:@"sourceApplication"];
  109. }
  110.  
  111. if (annotation) {
  112. [openURLData setValue:annotation forKey:@"annotation"];
  113. }
  114.  
  115. // [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
  116. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification object:openURLData]];
  117.  
  118. // Send notice to the rest of our plugin that we didn't handle this URL
  119. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"IonicLinksUnhandledURL" object:[url absoluteString]]];
  120. }
  121. return YES;
  122. }
  123.  
  124. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
  125. // Pass it off to our plugin
  126. IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
  127.  
  128. if(plugin == nil) {
  129. //If plugin is not available add onetime observer that will handle deeplink continuity when webview will appear
  130. __block id observer = nil;
  131. observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"CDVViewDidAppearNotification" object:nil queue:nil usingBlock: ^(NSNotification *block){
  132. if(observer != nil){
  133. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  134. observer = nil;
  135. }
  136. IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
  137. [plugin handleContinueUserActivity:userActivity];
  138. }];
  139. return NO;
  140. }
  141.  
  142. BOOL handled = [plugin handleContinueUserActivity:userActivity];
  143.  
  144. if(!handled) {
  145. // Continue sending the openURL request through
  146. }
  147. return YES;
  148. }
  149.  
  150. - (BOOL) checkWhiteList:(NSString *) sourceApplication {
  151. NSString *sysVer = [[UIDevice currentDevice] systemVersion];
  152. float versionIos = [sysVer floatValue];
  153.  
  154. NSLog(@"COME FROM : %@", sourceApplication);
  155. NSLog(@"COME FROM WITH IOS VERSION: %f", versionIos);
  156.  
  157. if (versionIos >= 13.0) {
  158. return YES;
  159. }
  160.  
  161. if([sourceApplication isEqual:@"jp.naver.line"] ||
  162. [sourceApplication isEqual: @"com.facebook.Messenger"] ||
  163. [sourceApplication isEqual: @"com.facebook.MessengerDev"] ||
  164. [sourceApplication isEqual: @"com.facebook.OrcaDevelopment"]||
  165. [sourceApplication isEqual: @"com.facebook.orca"]||
  166. [sourceApplication isEqual: @"com.ktbuniversity.wumobile"]||
  167. [sourceApplication isEqual: @"com.ktbuniversity.wumobileuat"]||
  168. [sourceApplication isEqual: @"com.ktbuniversity.wumobile.uat"]||
  169. [sourceApplication isEqual: @"com.ktbuniversity.mjumobile"]||
  170. [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.sit"]||
  171. [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.r3.sit"]||
  172. [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.uat"]||
  173. [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile"]||
  174. [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.uat"]||
  175. [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.r3.sit"]||
  176. [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.sit"]||
  177. [sourceApplication isEqual: @"com.pdmo.android.bonddirect"]){
  178. return YES;
  179. }else{
  180. return NO;
  181. }
  182. }
  183.  
  184. - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  185. [self application:application swizzledDidFinishLaunchingWithOptions:launchOptions];
  186.  
  187. if (![FIRApp defaultApp]) {
  188. [FIRApp configure];
  189. }
  190.  
  191. // [START set_messaging_delegate]
  192. [FIRMessaging messaging].delegate = self;
  193. // [END set_messaging_delegate]
  194. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  195. self.delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
  196. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  197. #endif
  198.  
  199. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
  200. name:kFIRInstanceIDTokenRefreshNotification object:nil];
  201.  
  202. self.applicationInBackground = @(YES);
  203.  
  204. if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
  205. NSDictionary *mutableUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  206.  
  207. double delayInSeconds = 5.0;
  208. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  209.  
  210. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  211. [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
  212. });
  213. }
  214.  
  215.  
  216. return YES;
  217. }
  218.  
  219. - (void)applicationDidBecomeActive:(UIApplication *)application {
  220. [self connectToFcm];
  221. self.applicationInBackground = @(NO);
  222. [[WL sharedInstance] hideSplashScreen];
  223. }
  224.  
  225. - (void)applicationDidEnterBackground:(UIApplication *)application {
  226. [[FIRMessaging messaging] disconnect];
  227. self.applicationInBackground = @(YES);
  228. NSLog(@"Disconnected from FCM");
  229. [[WL sharedInstance] showSplashScreen];
  230. }
  231.  
  232. - (void)applicationWillEnterForeground:(UIApplication *)application
  233. {
  234. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  235. [[WL sharedInstance] hideSplashScreen];
  236. }
  237.  
  238. - (void)tokenRefreshNotification:(NSNotification *)notification {
  239. // Note that this callback will be fired everytime a new token is generated, including the first
  240. // time. So if you need to retrieve the token as soon as it is available this is where that
  241. // should be done.
  242. NSString *refreshedToken = [[FIRInstanceID instanceID] token];
  243. NSLog(@"InstanceID token: %@", refreshedToken);
  244.  
  245. // Connect to FCM since connection may have failed when attempted before having a token.
  246. [self connectToFcm];
  247. [FirebasePlugin.firebasePlugin sendToken:refreshedToken];
  248. }
  249.  
  250. - (void)connectToFcm {
  251. [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
  252. if (error != nil) {
  253. NSLog(@"Unable to connect to FCM. %@", error);
  254. } else {
  255. NSLog(@"Connected to FCM.");
  256. NSString *refreshedToken = [[FIRInstanceID instanceID] token];
  257. NSLog(@"InstanceID token: %@", refreshedToken);
  258. }
  259. }];
  260. }
  261.  
  262. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  263. [FIRMessaging messaging].APNSToken = deviceToken;
  264. NSLog(@"deviceToken1 = %@", deviceToken);
  265. }
  266.  
  267. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  268. NSDictionary *mutableUserInfo = [userInfo mutableCopy];
  269.  
  270. [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
  271.  
  272. // Print full message.
  273. NSLog(@"%@", mutableUserInfo);
  274.  
  275. [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
  276. }
  277.  
  278. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  279. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  280.  
  281. NSDictionary *mutableUserInfo = [userInfo mutableCopy];
  282.  
  283. [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
  284. // Print full message.
  285. NSLog(@"%@", mutableUserInfo);
  286. completionHandler(UIBackgroundFetchResultNewData);
  287. [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
  288. }
  289.  
  290. // [START ios_10_data_message]
  291. // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
  292. // To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
  293. - (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
  294. NSLog(@"Received data message: %@", remoteMessage.appData);
  295.  
  296. // This will allow us to handle FCM data-only push messages even if the permission for push
  297. // notifications is yet missing. This will only work when the app is in the foreground.
  298. [FirebasePlugin.firebasePlugin sendNotification:remoteMessage.appData];
  299. }
  300.  
  301. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  302. NSLog(@"Unable to register for remote notifications: %@", error);
  303. }
  304.  
  305. // [END ios_10_data_message]
  306. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  307. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  308. willPresentNotification:(UNNotification *)notification
  309. withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  310.  
  311. [self.delegate userNotificationCenter:center
  312. willPresentNotification:notification
  313. withCompletionHandler:completionHandler];
  314.  
  315. if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
  316. return;
  317.  
  318. NSDictionary *mutableUserInfo = [notification.request.content.userInfo mutableCopy];
  319.  
  320. [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
  321.  
  322. // Print full message.
  323. NSLog(@"%@", mutableUserInfo);
  324.  
  325. completionHandler(UNNotificationPresentationOptionAlert);
  326. // [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
  327. }
  328.  
  329. - (void) userNotificationCenter:(UNUserNotificationCenter *)center
  330. didReceiveNotificationResponse:(UNNotificationResponse *)response
  331. withCompletionHandler:(void (^)(void))completionHandler
  332. {
  333. [self.delegate userNotificationCenter:center
  334. didReceiveNotificationResponse:response
  335. withCompletionHandler:completionHandler];
  336.  
  337. if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
  338. return;
  339.  
  340. NSDictionary *mutableUserInfo = [response.notification.request.content.userInfo mutableCopy];
  341.  
  342. [mutableUserInfo setValue:@YES forKey:@"tap"];
  343.  
  344. // Print full message.
  345. NSLog(@"Response %@", mutableUserInfo);
  346.  
  347. [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
  348.  
  349. completionHandler();
  350. }
  351.  
  352. // Receive data message on iOS 10 devices.
  353. - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
  354. // Print full message
  355. NSLog(@"%@", [remoteMessage appData]);
  356. }
  357. #endif
  358.  
  359. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement