Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "AppDelegate+FirebasePlugin.h"
- #import "FirebasePlugin.h"
- #import "Firebase.h"
- #import <IBMMobileFirstPlatformFoundationHybrid/IBMMobileFirstPlatformFoundationHybrid.h>
- #import <IBMMobileFirstPlatformFoundation/WLAnalytics.h>
- #import <objc/runtime.h>
- #import "IonicDeeplinkPlugin.h"
- #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- @import UserNotifications;
- // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
- // running iOS 10 and above. Implement FIRMessagingDelegate to receive data message via FCM for
- // devices running iOS 10 and above.
- @interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
- @end
- #endif
- #define kApplicationInBackgroundKey @"applicationInBackground"
- #define kDelegateKey @"delegate"
- @implementation AppDelegate (FirebasePlugin)
- #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- - (void)setDelegate:(id)delegate {
- objc_setAssociatedObject(self, kDelegateKey, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (id)delegate {
- return objc_getAssociatedObject(self, kDelegateKey);
- }
- #endif
- + (void)load {
- Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
- Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
- method_exchangeImplementations(original, swizzled);
- }
- - (void)setApplicationInBackground:(NSNumber *)applicationInBackground {
- objc_setAssociatedObject(self, kApplicationInBackgroundKey, applicationInBackground, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (NSNumber *)applicationInBackground {
- return objc_getAssociatedObject(self, kApplicationInBackgroundKey);
- }
- - (void)callDeepLink:(NSURL *) url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
- IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
- if(plugin == nil) {
- return;
- }
- BOOL handled = [plugin handleLink: url];
- if(!handled) {
- // Pass event through to Cordova
- [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object: url]];
- // Send notice to the rest of our plugin that we didn't handle this URL
- [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"IonicLinksUnhandledURL" object:[url absoluteString]]];
- }
- }
- - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
- NSMutableString *sourceApp = [[NSMutableString alloc] init];
- NSMutableString *annotation = [[NSMutableString alloc] init];
- if([options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey]) {
- sourceApp = [options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey];
- }
- if([options objectForKey:UIApplicationOpenURLOptionsAnnotationKey]) {
- annotation = [options objectForKey:UIApplicationOpenURLOptionsAnnotationKey];
- }
- return [self application:app openURL:url sourceApplication:sourceApp annotation:annotation];
- }
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
- IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
- if(![self checkWhiteList:sourceApplication]) {
- NSLog(@"NOT IN WHITELIST");
- return NO;
- }
- if(plugin == nil) {
- NSLog(@"plugin is null");
- __block id observer = nil;
- observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"CDVViewWillAppearNotification" object:nil queue:nil usingBlock: ^(NSNotification *block){
- if(observer != nil){
- [[NSNotificationCenter defaultCenter] removeObserver:observer];
- observer = nil;
- }
- [self callDeepLink:url sourceApplication:sourceApplication annotation:annotation];
- }];
- }
- BOOL handled = [plugin handleLink:url];
- if(!handled) {
- // Pass event through to Cordova
- NSMutableDictionary * openURLData = [[NSMutableDictionary alloc] init];
- [openURLData setValue:url forKey:@"url"];
- if (sourceApplication) {
- [openURLData setValue:sourceApplication forKey:@"sourceApplication"];
- }
- if (annotation) {
- [openURLData setValue:annotation forKey:@"annotation"];
- }
- // [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
- [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification object:openURLData]];
- // Send notice to the rest of our plugin that we didn't handle this URL
- [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"IonicLinksUnhandledURL" object:[url absoluteString]]];
- }
- return YES;
- }
- - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
- // Pass it off to our plugin
- IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
- if(plugin == nil) {
- //If plugin is not available add onetime observer that will handle deeplink continuity when webview will appear
- __block id observer = nil;
- observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"CDVViewDidAppearNotification" object:nil queue:nil usingBlock: ^(NSNotification *block){
- if(observer != nil){
- [[NSNotificationCenter defaultCenter] removeObserver:observer];
- observer = nil;
- }
- IonicDeeplinkPlugin *plugin = [self.viewController getCommandInstance:PLUGIN_NAME];
- [plugin handleContinueUserActivity:userActivity];
- }];
- return NO;
- }
- BOOL handled = [plugin handleContinueUserActivity:userActivity];
- if(!handled) {
- // Continue sending the openURL request through
- }
- return YES;
- }
- - (BOOL) checkWhiteList:(NSString *) sourceApplication {
- NSString *sysVer = [[UIDevice currentDevice] systemVersion];
- float versionIos = [sysVer floatValue];
- NSLog(@"COME FROM : %@", sourceApplication);
- NSLog(@"COME FROM WITH IOS VERSION: %f", versionIos);
- if (versionIos >= 13.0) {
- return YES;
- }
- if([sourceApplication isEqual:@"jp.naver.line"] ||
- [sourceApplication isEqual: @"com.facebook.Messenger"] ||
- [sourceApplication isEqual: @"com.facebook.MessengerDev"] ||
- [sourceApplication isEqual: @"com.facebook.OrcaDevelopment"]||
- [sourceApplication isEqual: @"com.facebook.orca"]||
- [sourceApplication isEqual: @"com.ktbuniversity.wumobile"]||
- [sourceApplication isEqual: @"com.ktbuniversity.wumobileuat"]||
- [sourceApplication isEqual: @"com.ktbuniversity.wumobile.uat"]||
- [sourceApplication isEqual: @"com.ktbuniversity.mjumobile"]||
- [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.sit"]||
- [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.r3.sit"]||
- [sourceApplication isEqual: @"com.ktbuniversity.mjumobile.uat"]||
- [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile"]||
- [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.uat"]||
- [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.r3.sit"]||
- [sourceApplication isEqual: @"com.ktbuniversity.ktbmobile.sit"]||
- [sourceApplication isEqual: @"com.pdmo.android.bonddirect"]){
- return YES;
- }else{
- return NO;
- }
- }
- - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- [self application:application swizzledDidFinishLaunchingWithOptions:launchOptions];
- if (![FIRApp defaultApp]) {
- [FIRApp configure];
- }
- // [START set_messaging_delegate]
- [FIRMessaging messaging].delegate = self;
- // [END set_messaging_delegate]
- #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- self.delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
- [UNUserNotificationCenter currentNotificationCenter].delegate = self;
- #endif
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
- name:kFIRInstanceIDTokenRefreshNotification object:nil];
- self.applicationInBackground = @(YES);
- if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
- NSDictionary *mutableUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- double delayInSeconds = 5.0;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
- });
- }
- return YES;
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- [self connectToFcm];
- self.applicationInBackground = @(NO);
- [[WL sharedInstance] hideSplashScreen];
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- [[FIRMessaging messaging] disconnect];
- self.applicationInBackground = @(YES);
- NSLog(@"Disconnected from FCM");
- [[WL sharedInstance] showSplashScreen];
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- // 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.
- [[WL sharedInstance] hideSplashScreen];
- }
- - (void)tokenRefreshNotification:(NSNotification *)notification {
- // Note that this callback will be fired everytime a new token is generated, including the first
- // time. So if you need to retrieve the token as soon as it is available this is where that
- // should be done.
- NSString *refreshedToken = [[FIRInstanceID instanceID] token];
- NSLog(@"InstanceID token: %@", refreshedToken);
- // Connect to FCM since connection may have failed when attempted before having a token.
- [self connectToFcm];
- [FirebasePlugin.firebasePlugin sendToken:refreshedToken];
- }
- - (void)connectToFcm {
- [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
- if (error != nil) {
- NSLog(@"Unable to connect to FCM. %@", error);
- } else {
- NSLog(@"Connected to FCM.");
- NSString *refreshedToken = [[FIRInstanceID instanceID] token];
- NSLog(@"InstanceID token: %@", refreshedToken);
- }
- }];
- }
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- [FIRMessaging messaging].APNSToken = deviceToken;
- NSLog(@"deviceToken1 = %@", deviceToken);
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
- NSDictionary *mutableUserInfo = [userInfo mutableCopy];
- [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
- // Print full message.
- NSLog(@"%@", mutableUserInfo);
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- NSDictionary *mutableUserInfo = [userInfo mutableCopy];
- [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
- // Print full message.
- NSLog(@"%@", mutableUserInfo);
- completionHandler(UIBackgroundFetchResultNewData);
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
- }
- // [START ios_10_data_message]
- // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
- // To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
- - (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
- NSLog(@"Received data message: %@", remoteMessage.appData);
- // This will allow us to handle FCM data-only push messages even if the permission for push
- // notifications is yet missing. This will only work when the app is in the foreground.
- [FirebasePlugin.firebasePlugin sendNotification:remoteMessage.appData];
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSLog(@"Unable to register for remote notifications: %@", error);
- }
- // [END ios_10_data_message]
- #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- willPresentNotification:(UNNotification *)notification
- withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
- [self.delegate userNotificationCenter:center
- willPresentNotification:notification
- withCompletionHandler:completionHandler];
- if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
- return;
- NSDictionary *mutableUserInfo = [notification.request.content.userInfo mutableCopy];
- [mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
- // Print full message.
- NSLog(@"%@", mutableUserInfo);
- completionHandler(UNNotificationPresentationOptionAlert);
- // [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
- }
- - (void) userNotificationCenter:(UNUserNotificationCenter *)center
- didReceiveNotificationResponse:(UNNotificationResponse *)response
- withCompletionHandler:(void (^)(void))completionHandler
- {
- [self.delegate userNotificationCenter:center
- didReceiveNotificationResponse:response
- withCompletionHandler:completionHandler];
- if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
- return;
- NSDictionary *mutableUserInfo = [response.notification.request.content.userInfo mutableCopy];
- [mutableUserInfo setValue:@YES forKey:@"tap"];
- // Print full message.
- NSLog(@"Response %@", mutableUserInfo);
- [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
- completionHandler();
- }
- // Receive data message on iOS 10 devices.
- - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
- // Print full message
- NSLog(@"%@", [remoteMessage appData]);
- }
- #endif
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement