关于友盟iOS推送的随笔

首先,友盟推送和其它推送差不多,调用起来也比较简单,大概看看文档即可,此处,只是记录一下详细步骤和遇到的一些问题,仅供参考。


一、导入API,开始推送之旅

点击此处进入友盟推送官网。

导入SDK:


二、根据文档开始写方法

三、处理相应地代理方法

1iOS8 以上,如果注册了UIMutableUserNotificationAction 和UIMutableUserNotificationCategory ,则相应地需要在友盟推送中添加Category ID,才有效,并且需要在handleActionWithIdentifier方法中实现

下面附上我写的方法,仅供参考;


- (void)setUMengWithLaunchOptions:(NSDictionary *)launchOptions {
//set AppKey and AppSecret
[UMessage startWithAppkey:AppKey launchOptions:launchOptions];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
/*!
* iOS 8 下拉多选按钮
* 只有在推送的时候设置categorys.identifier(友盟有一个设置Category ID)才能出现以下定义的按钮
*/
//register remoteNotification types
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1_identifier";
action1.title = @"点击打开APP";
action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮
action2.identifier = @"action2_identifier";
action2.title = @"此处打开相关页面";
action2.activationMode = UIUserNotificationActivationModeForeground;//UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
action2.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
action2.destructive = YES;

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"INTO";//这组动作的唯一标示,只有在推送的时候设置categorys.identifier才能出现
[categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextMinimal)];

UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:[NSSet setWithObject:categorys]];
[UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];

} else{
//register remoteNotification types
[UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert];
}
#else

//register remoteNotification types
[UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert];

#endif

//for log
[UMessage setLogEnabled:YES];

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[UMessage registerDeviceToken:deviceToken];
NSLog(@"token-----%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""]);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"----%@",userInfo);

NSString *alertStr = [NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]];
//关闭友盟自带的弹出框
[UMessage setAutoAlert:NO];

[UMessage didReceiveRemoteNotification:userInfo];

self.userInfo = userInfo;
//定制自定的的弹出框
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"
message:alertStr
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"前往", nil];

[alertView show];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//若自定义弹出框,必须加这句话
[UMessage sendClickReportForRemoteNotification:self.userInfo];
NSLog(@"--%d",buttonIndex);
if (buttonIndex == 1) {
Class class = NSClassFromString(@"TabviewViewController");
UIViewController *viewController = [[class alloc] init];
[self.rootV.navigationController pushViewController:viewController animated:YES];
}
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
NSLog(@"----ssdslad---%@",identifier);
if ([identifier isEqualToString:@"action1_identifier"]) {

}else if ([identifier isEqualToString:@"action2_identifier"]) {
Class class = NSClassFromString(@"TabviewViewController");
UIViewController *viewController = [[class alloc] init];
[self.rootV.navigationController pushViewController:viewController animated:YES];
}
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{

NSString *error_str = [NSString stringWithFormat: @"%@", err];
NSLog(@"Failed to get token, error:%@", error_str);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值