我是靠谱客的博主 孤独大象,最近开发中收集的这篇文章主要介绍关于友盟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);

}

最后

以上就是孤独大象为你收集整理的关于友盟iOS推送的随笔的全部内容,希望文章能够帮你解决关于友盟iOS推送的随笔所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部