Difference between revisions of "IOS Promotion 5.1.8"
From AdLocus
m (→AppDelegate.m) |
|||
| (42 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收益。 | 適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收益。 | ||
| − | + | 啟動優惠通知服務,請將相關程式碼加至對應處。 | |
| − | + | ||
== AppDelegate.m == | == AppDelegate.m == | ||
<syntaxhighlight lang="objc"> | <syntaxhighlight lang="objc"> | ||
#import "AppDelegate.h" | #import "AppDelegate.h" | ||
| + | #import "ViewController.h" | ||
#import <AdLocus/AdLocus.h> | #import <AdLocus/AdLocus.h> | ||
| + | #import <UserNotifications/UserNotifications.h> | ||
| + | |||
| + | @import UserNotifications; | ||
| + | @interface AppDelegate () <UNUserNotificationCenterDelegate> | ||
| + | @end | ||
| + | |||
@implementation AppDelegate | @implementation AppDelegate | ||
| + | NSString *const kGCMMessageIDKey = @"gcm.message_id"; | ||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | ||
| Line 16: | Line 23: | ||
[FIRApp configure]; | [FIRApp configure]; | ||
[FIRMessaging messaging].delegate = self; | [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. | // Open background fetch. | ||
| Line 24: | Line 51: | ||
} | } | ||
| + | // Compatible | ||
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; | UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; | ||
if (localNotif) { | if (localNotif) { | ||
| Line 30: | Line 58: | ||
} | } | ||
| − | + | // AdLocus Push Service. | |
| − | + | ||
| − | //AdLocus Push Service. | + | |
if (!self.pushService) { | if (!self.pushService) { | ||
self.pushService = [ALPushService sharedInstance]; | self.pushService = [ALPushService sharedInstance]; | ||
| Line 41: | Line 67: | ||
self.pushService.targeting = targeting; | self.pushService.targeting = targeting; | ||
[self.pushService setEnable:YES]; | [self.pushService setEnable:YES]; | ||
| + | // targeting.testMode = YES; | ||
| + | // [self.pushService test]; | ||
return YES; | return YES; | ||
} | } | ||
| + | </syntaxhighlight> | ||
| + | == AppDelegate.m 實作內容 == | ||
| + | === 敬請注意程式碼上有 ALPushService 開頭的方法都需要加入 === | ||
| + | <syntaxhighlight lang="objc"> | ||
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { | - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { | ||
[ALPushService didReceiveLocalNotification:notification]; | [ALPushService didReceiveLocalNotification:notification]; | ||
| Line 54: | Line 86: | ||
} | } | ||
| + | // LBS Fucntion Needed | ||
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { | - (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]; | [ALPushService performFetchWithCompletionHandler:completionHandler withUserinfo:userInfo]; | ||
| + | NSLog(@"userInfo: %@", userInfo); | ||
} | } | ||
| Line 61: | Line 99: | ||
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { | withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { | ||
| − | |||
NSDictionary *userInfo = notification.request.content.userInfo; | NSDictionary *userInfo = notification.request.content.userInfo; | ||
[[FIRMessaging messaging] appDidReceiveMessage:userInfo]; | [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; | ||
| Line 68: | Line 105: | ||
} | } | ||
| − | // | + | // Click Notification Event |
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { | - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { | ||
| Line 89: | Line 126: | ||
} | } | ||
| − | // FCM messaging token. | + | // 2020.07.23 update |
| − | - (void)messaging:( | + | // 註冊 FCM messaging token. |
| − | NSLog(@"FCM registration token: %@", fcmToken); | + | // 舊版使用 didRefreshRegistrationToken |
| − | + | - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken { | |
| + | NSLog(@"FCM registration token: %@", fcmToken); | ||
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | 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); | ||
} | } | ||
Latest revision as of 10:12, 27 August 2020
前言
適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收益。
啟動優惠通知服務,請將相關程式碼加至對應處。
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; }