iOS开发---本地通知(UILocalNotification)
来源:互联网 发布:mac怎么关掉客人用户 编辑:程序博客网 时间:2024/05/29 14:24
iOS中通知机制又叫消息机制,其包括两类:一类是本地通知;另一类是推送通知,也叫远程通知。
本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知。创建一个本地通知通常分为以下几个步骤:
- 创建UILocalNotification。
- 设置处理通知的时间fireDate。
- 配置通知的内容:通知主体、通知声音、图标数字等。
- 配置通知传递的自定义数据参数userInfo(这一步可选)。
- 调用通知,可以使用scheduleLocalNotification:按计划调度一个通知,也可以使用presentLocalNotificationNow立即调用通知。
简单实现一个本地通知应用,具体代码如下:
AppDelegate中:
#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; _window.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1.0]; //设置全局导航条风格和颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]]; [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; ViewController *VC = [[ViewController alloc]init]; _window.rootViewController = VC; [_window makeKeyAndVisible]; //如果已经获得发送通知哦的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置) if ([[UIApplication sharedApplication]currentUserNotificationSettings].types != UIUserNotificationTypeNone) { [self addLocalNotification]; }else{ [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; } return YES;}#pragma mark - **************** 调用过用户注册通知方法后执行(也就是调用完registerUserNotificationSettings:方法后执行)-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ if (notificationSettings.types != UIUserNotificationTypeNone) { [self addLocalNotification]; }}#pragma mark - **************** 进入前台后设置消息信息- (void)applicationWillEnterForeground:(UIApplication *)application { [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标}#pragma mark - **************** 私有方法//添加本地通知-(void)addLocalNotification{ //定义本地通知对象 UILocalNotification *notification = [[UILocalNotification alloc]init]; //设置调用时间 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];//通知触发时间,10s之后 notification.repeatInterval = 2; //通知重复次数 //设置通知属性 notification.alertBody = @"你要我怎样---薛之谦,点击查看-----";//通知主体 notification.applicationIconBadgeNumber = 1;//应用程序右上角显示的未读消息数 notification.alertAction = @"打开应用";//待机界面的滑动动作提示 notification.alertLaunchImage = @"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片 //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音 notification.soundName=@"msg.caf";//通知声音(需要真机才能听到声音) //设置用户信息 notification.userInfo = @{@"id":@1,@"user":@"XF"}; //调用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification];}#pragma mark - **************** 移除本地通知,在不需要此通知时记得移除-(void)removeNotification{ [[UIApplication sharedApplication] cancelAllLocalNotifications];}@end
注意:
- 在使用通知之前必须注册通知类型,如果用户不允许应用程序发送通知,则以后就无法发送通知,除非用户手动到iOS设置中打开通知。
- 本地通知是有操作系统统一调度的,只有在应用退出到后台或者关闭才能收到通知。
通知的声音是由iOS系统播放的,格式必须是Linear PCM、MA4(IMA/ADPCM)、µLaw、aLaw中的一种,并且播放时间必须在30s内,否则将被系统声音替换,同时自定义声音文件必须放到main boundle中。
- 本地通知的数量是有限制的,最近的本地通知最多只能有64个,超过这个数量将被系统忽略。
- 如果想要移除本地通知可以调用UIApplication的cancelLocalNotification:或cancelAllLocalNotifications移除指定通知或所有通知。
0 0
- iOS开发---本地通知(UILocalNotification)
- [IOS开发] 本地通知:UILocalNotification
- iOS开发之UILocalNotification本地通知
- iOS开发之本地通知UILocalNotification
- iOS本地通知UILocalNotification
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知 UILocalNotification
- iOS 本地通知:UILocalNotification
- iOS: 本地通知(UILocalNotification )的应用
- iOS开发之通知机制(一)本地通知(UILocalNotification)
- ListView(头部+下拉筛选+顶部悬停)
- 检测MySQL负载过高及解决方法(详解SSH TOP命令)
- RTMP流媒体播放过程
- Centos 建立用户
- Tkinter教程之Canvas篇(3)
- iOS开发---本地通知(UILocalNotification)
- CSS水平居中和垂直居中解决方案
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- CSS-DOM设计style属性的艺术
- 组合算法
- RecyclerView---高仿网易新闻客户端
- Python从入门到放弃(一):概论
- 不要轻易挑战用户的习惯,否则会被用户打脸!
- 如何判断链表中存在环路