로딩중입니다
[iOS] 定向推送 : Unity iOS
10/7/2015 3:11:50 PM

Live Operation 定向推送服务


LiveOps定向推送通过筛选应用用户, 向目标用户推送消息。

集成定向推送,应用运营人员通过精细化的推送系统,可以更便利地进行运营活动。

[LiveOperation 服务介绍]


注意事项
  1. 集成 Live Operations 之前,必须先完成IGAW共同集成。 [IGAW 共同集成 : Unity iOS]
  2. 为了集成 LiveOperation,需将 IgaworksUnityPlugin_iOS~*.unitypackage 文件包含在 Unity Project 内。[SDK 安装 : Unity]



基本设置

为了使用LiveOps服务,需要如下过程。


注册APNS认证书

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


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


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 服务,需修改位于Xcode中Classes组内的 UnityAppController.mm,以进行集成。

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

服务器推送是通过服务器发送 LiveOps 管理页面中登录的消息,客户端推送是使用 API 在客户端内生成推送消息。



基本集成 / 服务器推送

LiveOps 服务器推送在完成基本集成的情况下可以使用。


输入用户识别码

用户识别码,是在 LiveOps 中,为了辨别用户所使用的信息。


    注意事项

  1. 每一个用户只有一个固定的用户识别码,不可使用变量。
  2. 不可包含个人信息(有邮箱,姓名,电话,可识别的ID等)。*若需要使用的话,该值进行加密。
  3. 如需使用中文,韩文,空白等字符时,需对URL进行编码。
  4. 调用 LiveOpsInitPush API 前,需先进行设定。

意上面注意事项,输入用户识别码。

IgaworksCorePluginIOS.SetUserId("player1001");


初始化API调用

  1. SetUserId
  2. LiveOpsInitPush
  3. setDeviceToken : 在 Xcode Project 的 UntiyAppController.mm 文件中注册。
// Unity Project
public class MySampleScene : MonoBehavior {
 
    void Start () {
        //输入用户识别码。
	IgaworksCorePluginIOS.SetUserId("player1001");

        //输入用户识别码
LiveOpsPluginIOS.LiveOpsInitPush(); } }
// Xcode Project
#include <LiveOps/LiveOps.h>
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { AppController_SendNotificationWithArg(kUnityDidRegisterForRemoteNotificationsWithDeviceToken, deviceToken); UnitySendDeviceToken(deviceToken); //注册设备令牌(Token) [LiveOpsPush setDeviceToken:deviceToken]; }

SetUserId, LiveOpsInitPush api 在Unity Project 调用。

+ setDeviceToken api 在Unity Project中以Xcode建立后,在 UnityAppController.mm 文件中调用。

+ 用户识别码不能包括 (电子邮件,姓名,电话号码,可辨认的用户ID等)个人信息。

+ 在用户识别码包含韩文、特殊文字和空白等的情况下,一定要进行编码处理后才能使用。


Handle 登录

为了管理服务器推送和本地推送,需要注册handle。

需在Unity中用Xcode进行build之后,在Xcode Classes group内的UnityAppController.mm文件中,import LiveOps.h 来进行。

//LiveOps header 添加
#include <LiveOps/LiveOps.h>
- (void)application:(UIApplication*)application didReceiveNotification:(UILocalNotification*)notification { AppController_SendNotificationWithArg(kUnityDidReceiveLocalNotification, notification); UnitySendLocalNotification(notification); [LiveOpsPush handleLocalNotification:notification]; } #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0 #warning “Remote push open tracking is counted only when user touched notification center under iOS SDK 7.0” // iOS 版本未达到7时 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo); UnitySendRemoteNotification(userInfo); [LiveOpsPush handleRemoteNotification:userInfo fetchHandler:nil]; } #else // iOS 版本在7以上时 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo); UnitySendRemoteNotification(userInfo); [LiveOpsPush handleRemoteNotification:userInfo fetchHandler:completionHandler]; } #endif

+ 如上例所示,handle 需要以 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”}) Type 登录。


Info.plist 修改

为了使用Deep link)功能,需要在 Info.plist 添加如下节点。



确认 Deep Link 数据

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

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

  • 使用 LiveOps Listener
  • AppDelegate


使用 Live Ops 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 Parameter 信息,进行确认。

#import "AppDelegate.h"
#import <LiveOps/IgaworksAD.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       //运行的音频文件名,nil : iOS 基本声音播放
                               badgeNumber:0         //当未确认时, App的图标显示为未确认信息提醒数值
                                  customPayload:nil  //通过customPayload,注册的用户信息
];    

取消推送

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

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



LiveOps追加选项


目标用户定位

设置用户群,可以直接选择接收推送的对象。
可以直接设置数据,建立用户群。

目标定位数据设置

为了只对目标用户推送消息,可以设置自定义用户数据。
设定的自定义用户数据,可以在推送管理页面进行确认。 以下示例是以用户年龄为自定义数据。
//LiveOpsSetTargetingNumberData(int customUserData, string customUserDataKey);
//LiveOpsSetTargetingStringData(string customUserData, string customUserDataKey);

LiveOpsPluginIOS.LiveOpsSetTargetingNumberData(86, "age");
customUserData : 输入用户数据进行目标定位。
customUserDataKey : 设置用户自定义数据 Key。

Push 接收 on/off

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

Push接收设置

调用 LiveOpsSetRemotePushEnable API,使用示例如下。
  • true : 接收推送
  • false : 不接受推送
LiveOpsPluginIOS.LiveOpsSetRemotePushEnable(false);