로딩중입니다
[iOS] 定向推送 : iOS
8/25/2015 12:32:31 PM

Live Operation 定向推送服务

 

LiveOps 定向推送是通过筛选应用的用户,发送定向推送消息。

集成推送功能,运营可以使用精化的推送系行更便利的运营

[LiveOperation 服务介绍]


注意事项
  1. 集成 LiveOperation add-on 前,必须集成 IGAW 共同集成文件。 [IGAW 共同集成 : iOS]
  2. 为集成 LiveOperation 需将 LiveOps~*.framework 文件添加至 Xcode Project。  [SDK 安装 : iOS]



基本设置

使用 LiveOps 服务须完成以下事项


注册 APNS 认证书

Apple Developer Center 获取 APNS 认证书,将其登录在 LiveOps 管理页面中。

 

+ 开发版本上登 Development 版本,提交AppStore 的版本 .p12 形式的 Production 认证书

+ 需登录 .p12 扩展名的认证书。


Info.plist 设置

开发环境的 iOS SDK Target 处于 7.0 以上时,请将以下节点添加进 Info.plist。


Xcode 设置

为了激活推送服务,在 Project > Capablities 项目上激活功能。


Push Service 激活


Background Mode 激活


添加 NotificationDelegate 文件

为了正常收到推送信息,请在 Project 上添加 NotificationDelegate 文件。


NotificationDelegate.h

#ifndef NotificationDelegate_h
#define NotificationDelegate_h

@import UserNotifications;
  #import <LiveOps/LiveOpsPush.h>


@interface NotificationDelegate : NSObject <UNUsernotificationcenterdelegate>

@end
  #endif /* NotificationDelegate_h */

NotificationDelegate.m

#import <foundation/foundation.h>
#import "NotificationDelegate.h"
#import <LiveOps/LiveOps.h>


@implementation NotificationDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
    
    [LiveOpsPush handleUserNotificationCenter:center
               didReceiveNotificationResponse:response];
    completionHandler();
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    
    NSDictionary* userInfo = [notification.request.content.userInfo mutableCopy];
    [LiveOpsPush handleUserNotificationCenter:center
                      willPresentNotification:notification
                willShowSystemForegroundAlert:YES];
    
    
    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}

@end

修改 AppDelegate.h 文件

为了正常收到推送信息,需修改 AppDelegate 文件。


AppDelegate.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
#define XC8_AVAILABLE 1
#import <UserNotifications/UserNotifications.h>
#import "NotificationDelegate.h"
#endif

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

#if XC8_AVAILABLE
NotificationDelegate *notificationDelegate;
#endif
}

@property (strong, nonatomic) UIWindow *window;


@end




LiveOps API

使用 LiveOps 服务,Import LiveOps.h, IgaworksCore.h 文件进行集成。

LiveOps 支持 服务器推送 和 客户端推送


器推送是LiveOps 管理面中登消息端推送是使用 API 在客生成推送消息。


基本集成 / 服务器推送

LiveOps 服务器推送完成基本集成即可使用,为了完成集成请在 AppDelegate 的 appliation: didFinishLaunchingWithOptions:  application: didRegisterForRemoteNotificationWithiDeviceToken 中添加以下代码。


输入用户识别码

用户识别码是 LiveOps 判断用户使用的信息。.


    注意事项

  1. 每一只有一固定的用户识别码,不可使用量。
  2. 不可包含人信息(箱,姓名,电话,可识别ID)
  3. 如需使用中文,文,空白等字符,需URL编码
  4. 最大 nvarchar(100)。

留意以上注意事入用户识别码

[IgaworksCore setUserId:@"user10001"];


调用初始化 API

  1. + (void)setUserId:(NSString *)userId
  2. + (void)initPush
  3. + (void)handleAllNotificationFromLaunch:(NSDictionary*)launchOptions
  4. + (void)setDeviceToken:(NSData*)deviceToken
#import <IgaworksCore/IgaworksCore.h>
#import <LiveOps/LiveOps.h>
 
@implementation AppDelegate
 
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    //IGAWorks 共同集成 API
    [IgaworksCore igaworksCoreWithAppKey:@"设置 AppKey" andHashKey:@"设置 HashKey"  andIsUseIgaworksRewardServer:NO];
 
    //为使用 LiveOpsPush 必须设置 user id
    [IgaworksCore setUserId:@"user10001"];
 
    //初始化 LiveOpsPush
    [LiveOpsPush initPush];
 
    //登录 LiveOps Push Notification Handler
    [LiveOpsPush handleAllNotificationFromLaunch:launchOptions]; 
    
    return YES;
}
 
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //登录 Device Token
    [LiveOpsPush setDeviceToken:deviceToken];
}
 
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    //Device Token 登录失败 Log 确认
    NSLog(@"Device Token Register Failed: %@", error);
}


登录 Handler

登录 Handler 管理推送。

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0
#warning "Remote push open tracking is counted only when user touches notification center under iOS SDK 7.0"
 
// iOS version7 以下时
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [LiveOpsPush handleRemoteNotification:userInfo fetchHandler:nil];
}
#else
// iOS version7 以上时
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [LiveOpsPush handleRemoteNotification:userInfo fetchHandler:completionHandler];
}
#endif

//Local Push Handler
- (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification *)notification
{
	[LiveOpsPush handleLocalNotification:notification];
}

+ 照以上示例代码,Handler 需以 iOS7 基准,根据 OS 版本分



Deep Link 集成 / 服务器推送

LiveOps 的服务器推送服务提供 Deep Link 功能。

Deep Link 功能是在用收到推送消息,可以Deep Link 据所指定的作。

 

Deep Link 数据是在 LiveOps 管理页面中创建和管理,可用网页 URL (http://~) Type,App Schema URL (myApp://deepLinkAction) Type 或 Json ({“url”:”deepLinkAction”}形式登录。

 

Info.plist 修改

使用 Deep Link 请参考以下容在 Info.plist 添加 node。



确认 Deep Link 数据。

确认 Deep Link 数据的方法可以使用 LiveOps Listener 或使用 AppDelegate

使用 Json Type 的 Deep Link 时,只能使用 LiveOps Listener 方式。

  • 使用 LiveOps Listener
  • AppDelegate


使用 LiveOps Listener

使用 listener handleAllNotificationFromLaunch: API 前,首先登录 Listener。

 Listener pushInfos Deep Link情如下

    1. pushInfos.sentTime : 发送的时间
    2. pushInfos.bodyText : 推送内容
    3. pushInfos.deepLinkUrl : Apple Scheme Url Type 的 Deep Link,基本价 nil
    4. pushInfos.deepLink : Json Type 的 Deep Link,基本价 nil
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
	// 为使用 Deep Link 登录 Listener
	[LiveOpsPush setRemoteNotificationListener:^(NSArray* pushInfos, BOOL isForeGround) {
		for(LiveOpsPushInfo *pushInfo in pushInfos){
			/* pushInfos 客体 */
			// pushInfo.sentTime : 发送的时间
			// pushInfo.bodyText : 推送内容
			// pushInfo.deepLinkUrl : Deep Link URL,基本值-nil 
			// pushInfo.deepLink : Deep Link JsonObject,基本值-nil
			
			/* isForeGround */
			// YES : app 运行时接收推送
			// NO : app 未运行时接收推送
		}       
	}];
	
	//登录 LiveOps Push Notification Handler
	[LiveOpsPush handleAllNotificationFromLaunch:launchOptions];
	
   return YES;
}

使用 AppDelegate

使用 Apple Scheme Url Type 的 Deep Link 时,通过 AppDelegate 可以确认 Deep Link 信息。

使用 application: openUrl: sourceApplication: annotation: Delegate 的 URL 参数信息,进行确认。

#import "AppDelegate.h"
#import <IgaworksCore/IgaworksCore.h>
#import <LiveOps/LiveOps.h>
 
@implementation AppDelegate
 
-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(idD)annotation
{
    //这次传达推送的 Deep Link
    //使用确认的 Deep Link 信息,执行需要的运作。
    NSLog(@"LiveOps Deep Link Url Info : %@", [url absoluteString]);
 
    //ex. Deep Link 是 liveOps://com.igaworks.test?view=myview&param=1 时
    NSString* query = [url query];
    NSArray *queryPairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *pairs = [NSMutableDictionary dictionary];
    for(NSString* queryPair in queryPairs)
    {
        NSArray *bits = [queryPairs componentsSeparatedByString:@"="];
        NSString *key = [bits objectAtIndex:0];
        NSString *value = [bits objectAtIndex:1];
        [pairs setObject:value forKey:key];
    
        NSLog(@"LiveOps Deep Link Action~!! key: %@ , value: %@" , key, value);
    }
    return YES;
}

+ AppDelegate 方式不可使用 Json Type 的 Deep Link。



本地推送 (客户端推送)

LiveOps 提供客端推送功能,使用 API 可以直接在端生成推送
普通情下客端推送是内当匹配指定,生成推送消息曝光
开发商服器和用互,可以通过获取服event 生成推送的方式


生成和曝光推送消息

使用以下 API 生成曝光推送消息。
[LiveOpsPush registerLocalPushNotification:localPid  //唯一的识别
                                      date:dateObj   //日期
                                      body:@"hello"  //local push 要显示的文字
                                    button:@"go:     //local push 确认按钮或锁屏按钮测试
                                 soundName:nil       //运行的 Sound File 名称,nil : iOS 播放基本 Sound
                               badgeNumber:0         //未确认消息时应用图标上标示的未确认信息通知数量。
                                  customPayload:nil  //通过 customPayload 登录的用户信息。
];     
 

取消推送

使用 cancelLocalPush: API 取消生成的本地推送。

[LiveOpsPush cancelLocalPush:localPid]; //localPid : 登录 local push 时输入的推送 ID

 

Listener

为了使用 Listener 必须在 handleAllNotificationFromLaunch: API 之前,首先登录 Listener。

首先登录 Listener。

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
	[LiveOpsPush setLocalNotificationListener:^(NSInteger Id, NSDate* sentTime, NSString* bodyText, NSDictionary* customData, BOOL isForeGround) {
			// Id : localPid : 登录 local push 时输入的 Push ID
			// sentTime : 发送信息的时间
			// bodyText : Push 内容
			// customData : 通过 customPayload 登录的 data

			
			/* isForeGround */
			// YES : app 启动状态时接收推送
			// NO : app 关闭状态时接收推送
	}];
	
	//登录 LiveOps Push Notification Handler
	[LiveOpsPush handleAllNotificationFromLaunch:launchOptions];
	
   return YES;
}

 



设置定向数据


用户定向

设定用户群,可以直接选择目标用户。
为设定用户群可以直接设置数据。

设置定向数据

为了只对定向用户发送定向消息,需自行设置用户数据。
设置的自定义用户数据可在定向推送的管理页面中确认。以下示例是用户年龄为自定义数据。
//[LiveOpsUser setTargetingData:(NSString*)customUserData withKey:(NSString*)customUserDataKey]
[LiveOpsUser setTargetingData:@16 withKey:@"age"];
customUserData : 输入要定向的用户数据
customUserDataKey : 设置用户自定义数据 Key

呼叫 Targeting 数据

呼叫设置的 Targeting 数据执行需要的动作。
NSNumber* ageNum = [LiveOpsUser getTargetingDataWithKey:@"age"];

同步 Targeting 数据

调用 flush api,将 Targeting 数据同步到 LiveOps 服务器。
[LiveOpsUser flush];

 


Push on/off

可以设置是否接受推送消息。一般情况下,用户在设置是否接受推送消息时,调用 API。
并对于接收消息与否提供 Delegate。使用 Event Delegate,处理消息接收与否。

设置 Push 接收

调用 setRemotePushEnable API,使用示例如下。
  • true : 接收推送
  • false : 不接受推送
[LiveOpsPush setRemotePushEnable:YES];



使用 Delegate

对于是否接受传达的推送消息提供 Delegate。
调用 setRemotePushEnable API,Parameter 使用示例如下。

[LiveOpsPush setRemotePushEnable:NO completion:^(BOOL result) {
	//result 值若不是 true 的话,即服务器上设置错误
	//(网络问题或设备 Token 登录失败时)
	if (result) {
		//使用接受的信息,执行需要的动作
	}
}];