Difference between revisions of "IOS Promotion 5.1.8"

From AdLocus
Jump to: navigation, search
(Created page with "== 前言 == 適地性優惠通知服務是依據全區定向或特定地理位置定向,提供您的使用者最適合的主動式訊息,同時具有高額的分潤收...")
 
(AppDelegate.m)
Line 17: Line 17:
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
     ALPushService *push = [ALPushService sharedInstance];
+
     // Open Firebase Service
     push.key = @"請填入您的APP KEY";
+
    [FIRApp configure];
    push.enable = YES;
+
     [FIRMessaging messaging].delegate = self;
    // [push test]; //五秒後會出現測試廣告,可測試是否整合成功,正式發佈前須關閉。
+
  
     [application setMinimumBackgroundFetchInterval:60 * 60];
+
     //  Open background fetch.
 +
    NSString *sysVersion = [UIDevice currentDevice].systemVersion;
 +
    CGFloat version = [sysVersion floatValue];
 +
    if (version >= 7.0) {
 +
        [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:1 * 60];
 +
    }
  
     UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
+
     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
     if (localNotification) {
+
     if (localNotif) {
         [ALPushService didReceiveLocalNotification:localNotification];
+
         [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;
 
     return YES;
 
}
 
}
Line 38: Line 55:
 
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
 
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
 
     [ALPushService performFetchWithCompletionHandler: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;
 
}
 
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 10:07, 5 June 2020

前言

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

啟動優惠通知服務,請將相關程式碼加在 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;
}