IOS Promotion 5.1.8

From AdLocus
Jump to: navigation, search

前言

適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收益。

啟動優惠通知服務,請將相關程式碼加在 AppDelegate 處。

實作

  • -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  • -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
  • -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

AppDelegate.m

#import "AppDelegate.h"
#import <AdLocus/AdLocus.h>
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    // Open Firebase Service
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
 
    //  Open background fetch.
    NSString *sysVersion = [UIDevice currentDevice].systemVersion;
    CGFloat version = [sysVersion floatValue];
    if (version >= 7.0) {
        [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:1 * 60];
    }
 
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        [ALPushService didReceiveLocalNotification:localNotif];
        [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
    }
 
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
 
    //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];
 
    return YES;
}
 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [ALPushService didReceiveLocalNotification:notification];
}
 
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [ALPushService performFetchWithCompletionHandler:completionHandler];
    completionHandler(UIBackgroundFetchResultNewData);
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [ALPushService performFetchWithCompletionHandler:completionHandler withUserinfo:userInfo];
}
 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification 
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
 
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    NSDictionary *userInfo = notification.request.content.userInfo;
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
 
    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}
 
//點擊後事件
- (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();
    }
}
 
// FCM messaging token.
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    self.pushService.FCMkey = fcmToken;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setObject:fcmToken forKey:@"fcmKey"];
}
 
// FCM registration token.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"APNs device token retrieved: %@", deviceToken);
    // With swizzling disabled you must set the APNs device token here.
    [FIRMessaging messaging].APNSToken = deviceToken;
}