Live Operation 定向推送服务
LiveOps 定向推送是通过筛选应用的用户,发送定向推送消息。
集成推送功能,运营人员可以使用精细化的推送系统,进行更便利的运营活动。
注意事项
- 集成 LiveOperation add-on 前,必须集成 IGAW 共同集成文件。 [IGAW 共同集成 : iOS]
- 为集成 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 文件。
#ifndef NotificationDelegate_h
#define NotificationDelegate_h
@import UserNotifications;
#import <LiveOps/LiveOpsPush.h>
@interface NotificationDelegate : NSObject <UNUsernotificationcenterdelegate>
@end
#endif /* NotificationDelegate_h */
NotificationDelegate.m
#ifndef NotificationDelegate_h
#define NotificationDelegate_h
@import UserNotifications;
#import <LiveOps/LiveOpsPush.h>
@interface NotificationDelegate : NSObject <UNUsernotificationcenterdelegate>
@end
#endif /* NotificationDelegate_h */
#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
#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
#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 服务器推送完成基本集成即可使用,为了完成集成请在 AppDelegate 的 appliation: didFinishLaunchingWithOptions: 和 application: didRegisterForRemoteNotificationWithiDeviceToken: 中添加以下代码。
输入用户识别码
用户识别码是 LiveOps 判断用户使用的信息。.
注意事项
- 每一个用户只有一个固定的用户识别码,不可使用变量。
- 不可包含个人信息(有邮箱,姓名,电话,可识别的ID等)。
- 如需使用中文,韩文,空白等字符时,需对URL进行编码。
- 最大长度为 nvarchar(100)。
请留意以上注意事项,输入用户识别码。
[IgaworksCore setUserId:@"user10001"];
调用初始化 API
- + (void)setUserId:(NSString *)userId
- + (void)initPush
- + (void)handleAllNotificationFromLaunch:(NSDictionary*)launchOptions
- + (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,详情如下:
- pushInfos.sentTime : 发送的时间
- pushInfos.bodyText : 推送内容
- pushInfos.deepLinkUrl : Apple Scheme Url Type 的 Deep Link,基本价 nil
- 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¶m=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 生成推送的方式进行。
生成和曝光推送消息
[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"];
呼叫 Targeting 数据
NSNumber* ageNum = [LiveOpsUser getTargetingDataWithKey:@"age"];
同步 Targeting 数据
[LiveOpsUser flush];
Push on/off
可以设置是否接受推送消息。一般情况下,用户在设置是否接受推送消息时,调用 API。并对于接收消息与否提供 Delegate。使用 Event Delegate,处理消息接收与否。
设置 Push 接收
调用 setRemotePushEnable API,使用示例如下。- true : 接收推送
- false : 不接受推送
[LiveOpsPush setRemotePushEnable:YES];
- true : 接收推送
- false : 不接受推送
[LiveOpsPush setRemotePushEnable:YES];
使用 Delegate
对于是否接受传达的推送消息提供 Delegate。
调用 setRemotePushEnable API,Parameter 使用示例如下。
[LiveOpsPush setRemotePushEnable:NO completion:^(BOOL result) {
//result 值若不是 true 的话,即服务器上设置错误
//(网络问题或设备 Token 登录失败时)
if (result) {
//使用接受的信息,执行需要的动作
}
}];
对于是否接受传达的推送消息提供 Delegate。
调用 setRemotePushEnable API,Parameter 使用示例如下。
[LiveOpsPush setRemotePushEnable:NO completion:^(BOOL result) {
//result 值若不是 true 的话,即服务器上设置错误
//(网络问题或设备 Token 登录失败时)
if (result) {
//使用接受的信息,执行需要的动作
}
}];