本地推送

1、iOS8引入了地点通知(location notifications)。它其实也是本地通知(local notifications),但是他们只会在用户一个特定的地理或者iBeacon区域时,才会被触发。虽然我们看不到什么细节,地点通知 (location notifications)实现起来也很容易。

2、从iOS8开始,通知被加入了新的特性。简单地说,从现在开始,当一个通知被展示时,开发者可以指定用户可触发的具体的动作(actions),而且甚至不用启动App也可以处理这个通知。动作(Actions)可以被归类到一个类目(categories)下。特别是当App安排了不少通知显示的时候相当方便。用类目 (categories),一个通知所有相关的动作(actions)都可以一次性的被捆绑和指定。反之,处理动作(actions)也非常简单,只需要实现其代理方法即可。每一个动作(actions)都有一个特殊的属性标示符(identifier),被App用来辨别收到的动作(actions)然后适当地处理它。

3、可以被安排的本地通知的数量并不是无限的,最多有64个本地通知可以被安排和展示。如果多余这个数字,所有超过这个数字的通知都会被废弃。尽管如此,无论通知是以什么样的形式被安排的,最早的那个会被最先展示。

4、具体代码:

在ViewController.m中添加代码:

//创建通知

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

//滑动提醒

localNotification.alertAction = @"查看消息";

localNotification.hasAction = YES;

//提醒内容

localNotification.alertBody = @"推送消息";

//category的identifier

localNotification.category = @"COMPLETE_CATEGORY";

//推送声音

localNotification.soundName = nil;

//触发时间

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];

//应用程序右上角图标标记

localNotification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在AppDelegate.m中添加代码:

在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加

UIMutableUserNotificationAction *notificationActionOk = [[UIMutableUserNotificationAction alloc]init];

notificationActionOk.identifier = @"completeRemindRater";

notificationActionOk.title = @"再工作一会儿";

//是否取消提醒

notificationActionOk.destructive = NO;

//是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行

notificationActionOk.authenticationRequired = NO;

//启动app还是后台执行

notificationActionOk.activationMode = UIUserNotificationActivationModeBackground;

notificationActionOk.behavior = UIUserNotificationActionBehaviorDefault;

UIMutableUserNotificationAction *notificationActionRestNow = [[UIMutableUserNotificationAction alloc]init];

notificationActionRestNow.identifier = @"relaxNow";

notificationActionRestNow.title = @"休息";

//是否取消提醒  当设置为yes时,通知中相应地按钮的背景色会变成红色

notificationActionRestNow.destructive = YES;

//是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行

notificationActionRestNow.authenticationRequired = NO;

//启动app还是后台执行

notificationActionRestNow.activationMode = UIUserNotificationActivationModeBackground;

notificationActionRestNow.behavior = UIUserNotificationActionBehaviorDefault;

UIMutableUserNotificationCategory *notificationCompleteCategory = [[UIMutableUserNotificationCategory alloc] init];

notificationCompleteCategory.identifier = @"COMPLETE_CATEGORY";

[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextMinimal];

[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextDefault];

UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];

replyAction.title = @"回复";

replyAction.identifier = @"inline-reply";

replyAction.activationMode = UIUserNotificationActivationModeBackground;

replyAction.authenticationRequired = NO;

replyAction.behavior = UIUserNotificationActionBehaviorTextInput;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];

notificationCategory.identifier = @"REPLY_CATEGORY";

[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextMinimal];

[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge categories:[NSSet setWithArray:@[notificationCompleteCategory,notificationCategory]]]];

实现协议方法:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{

if ([identifier isEqualToString:@"completeRemindRater"]) {

NSLog(@"点击的是%@",identifier);

}

if ([identifier isEqualToString:@"relaxNow"]) {

NSLog(@"点击的是%@",identifier);

}

if ([identifier isEqualToString:@"inline-reply"]) {

NSLog(@"点击的是%@",identifier);

}

completionHandler();

}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

NSLog(@"接收到通知");

}

参考链接:

http://www.cocoachina.com/ios/20150112/10901.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS中通知机制又叫消息机制,其包括两类:一类是本地通知;另一类是推送通知,也叫远程通知。两种通知在iOS中...
    七里汀阅读 6,340评论 3 2
  • 一、简介 目前说的是基于ios10之前,所以有些类是过期的. 推送分为本地推送和远程推送2种。可以在应用没有打开甚...
    JingYa_Lu阅读 4,432评论 0 0
  • - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn ...
    nothing_c阅读 1,369评论 0 0
  • iOS本地推送 第一步:创建本地推送 // 创建一个本地推送 UILocalNotifica...
    学无止境666阅读 4,866评论 2 5
  • Test Trettff
    pku76阅读 1,265评论 0 0

友情链接更多精彩内容