IOS Promotion 5.1.8
From AdLocus
前言
適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收益。
啟動優惠通知服務,請將相關程式碼加至對應處。
AppDelegate.m
#import "AppDelegate.h" #import "ViewController.h" #import <AdLocus/AdLocus.h> #import <UserNotifications/UserNotifications.h> @import UserNotifications; @interface AppDelegate () <UNUserNotificationCenterDelegate> @end @implementation AppDelegate NSString *const kGCMMessageIDKey = @"gcm.message_id"; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Open Firebase Service [FIRApp configure]; [FIRMessaging messaging].delegate = self; if ([UNUserNotificationCenter class] != nil) { // iOS 10 or later // For iOS 10 display notification (sent via APNS) [UNUserNotificationCenter currentNotificationCenter].delegate = self; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; } else { // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications. UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; } [application registerForRemoteNotifications]; // Open background fetch. NSString *sysVersion = [UIDevice currentDevice].systemVersion; CGFloat version = [sysVersion floatValue]; if (version >= 7.0) { [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:1 * 60]; } // Compatible UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { [ALPushService didReceiveLocalNotification:localNotif]; [[UIApplication sharedApplication] cancelLocalNotification:localNotif]; } // AdLocus Push Service. if (!self.pushService) { self.pushService = [ALPushService sharedInstance]; self.pushService.key = @"<AdLocus app key>"; self.pushService.FCMAPIkey = @"<FCM API KEY>"; ALTargeting *targeting = [ALTargeting new]; self.pushService.targeting = targeting; [self.pushService setEnable:YES]; // targeting.testMode = YES; // [self.pushService test]; return YES; }
AppDelegate.m 實作內容
敬請注意程式碼上有 ALPushService 開頭的方法都需要加入
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [ALPushService didReceiveLocalNotification:notification]; } - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [ALPushService performFetchWithCompletionHandler:completionHandler]; completionHandler(UIBackgroundFetchResultNewData); } // LBS Fucntion Needed - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); } // debug for BreakPoint [ALPushService performFetchWithCompletionHandler:completionHandler withUserinfo:userInfo]; NSLog(@"userInfo: %@", userInfo); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound); } // Click Notification Event - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSDictionary * userInfo = response.notification.request.content.userInfo; NSURL *ourURL = [NSURL URLWithString:[userInfo objectForKey:@"ad_link"]]; if (ourURL) { dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] openURL:ourURL]; }); } completionHandler(); } else { [ALPushService didReceiveLocalNotification:response.notification]; completionHandler(); } } // 2020.07.23 update // 註冊 FCM messaging token. // 舊版使用 didRefreshRegistrationToken - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken { NSLog(@"FCM registration token: %@", fcmToken); NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:fcmToken forKey:@"fcmKey"]; self.pushService.FCMkey = fcmToken; } // 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); } // FCM registration token. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"APNs device token retrieved: %@", deviceToken); [FIRMessaging messaging].APNSToken = deviceToken; }