iOS推送:本地通知UILocalNotification

Notification是智能手机应用编程中 很是经常使用的一种传递信息的机制,并且能够很是好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不一样的 Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只须要将封装好 的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只 需设计好处理Notification的方法就完成了整个Notification流程了。编程

本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了全部处 理Notification须要的内容。UILocalNotification的属性有fireDate、timeZone、 repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。api

 

 

UILocalNotification的调度数组

其中fireDate、timeZone、repeatInterval和repeatCalendar是用于 UILocalNotification的调度。fireDate是UILocalNotification的激发的确切时间。timeZone是 UILocalNotification激发时间是否根据时区改变而改变,若是设置为nil的话,那么UILocalNotification将在一段时 候后被激发,而不是某一个确切时间被激发。 repeatInterval是UILocalNotification被重复激发之间的时间差,不过期间差是彻底根据日历单位 (NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,若是不设置的话,将不会重复激发。 repeatCalendar是UILocalNotification重复激发所使用的日历单位须要参考的日历,若是不设置的话,系统默认的日历将被做 为参考日历。服务器

UILocalNotification的提醒内容并发

alertBody、alertAction、hasAction和alertLaunchImage是当应用不在运行时,系统处理app

 

一、增长一个本地推送ide

//设置20秒以后ui

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];spa

/*设计

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"HH:mm:ss"];

NSDate *now = [formatter dateFromString:@"15:00:00"];//触发通知的时间

*/

//chuagjian

一个本地推送

UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];

if (noti) {

//设置推送时间

noti.fireDate = date;//=now

//设置时区

noti.timeZone = [NSTimeZone defaultTimeZone];

//设置重复间隔

noti.repeatInterval = NSWeekCalendarUnit;

//推送声音

noti.soundName = UILocalNotificationDefaultSoundName;

//内容

noti.alertBody = @"推送内容";

//显示在icon上的红色圈中的数子

noti.applicationIconBadgeNumber = 1;

//设置userinfo 方便在以后须要撤销的时候使用

NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];

noti.userInfo = infoDic;

//添加推送到uiapplication

UIApplication *app = [UIApplication sharedApplication];

[app scheduleLocalNotification:noti];

}

 

二、程序运行时接收到本地推送消息

 

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

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app"

message:notification.alertBody

delegate:nil

cancelButtonTitle:@"肯定"

otherButtonTitles:nil];

[alert show];

//这里,你就能够经过notification的useinfo,干一些你想作的事情了

application.applicationIconBadgeNumber -= 1;

}

 

三、取消一个本地推送

 

UIApplication *app = [UIApplication sharedApplication];

 

//获取本地推送数组

NSArray *localArr = [app scheduledLocalNotifications];

 

//声明本地通知对象

UILocalNotification *localNoti;

 

if (localArr) {

for (UILocalNotification *noti in localArr) {

NSDictionary *dict = noti.userInfo;

if (dict) {

NSString *inKey = [dict objectForKey:@"key"];

if ([inKey isEqualToString:key]) {

if (localNoti){

[localNoti release];

localNoti = nil;

}

localNoti = [noti retain];

break;

}

}

}

 

//判断是否找到已经存在的相同key的推送

if (!localNoti) {

//不存在 初始化

localNoti = [[UILocalNotification alloc] init];

}

 

if (localNoti && !state) {

//不推送 取消推送

[app cancelLocalNotification:localNoti];

[localNoti release];

return;

}

}

4.两种方式取消注册的本地通知,一种是取消指定的通知,第二种是取消全部的注册通知:

[[UIApplication sharedApplication] cancelLocalNotification:localNotification];

[[UIApplication sharedApplication] cancelAllLocalNotification];

5.iOS5的四种通知类型

 

5.1. 横幅(Banner)

横幅通知是在iOS5中出现的新特性,是显示在屏幕顶部的横条,几秒钟后会自动消失。一条横幅通知会显示程序的小图标(低分屏下显示 29×29的图标,高分屏显示58×58的图标),程序的名字和通知的内容。小图标能够帮助用户一眼就看清楚是 哪个应用程序在提醒他们。

 

5.2. 提醒(Alert)

提醒通知不会自动消失,须要用户与之交互才能关闭。设计师须要设计通知的具体内容,有时还要action button 设计title。整个提醒通知的背景样式,包括里面的按钮的样式都是不可变的,因此设计师和开发者就不要在这里发挥创意了。Android因为其开放性, 咱们是可使用本身设计的控件的,而iOS这样作多是为了保持UI风格一致性。

5.3. 标记(Badge)

标记通知是显示在程序图标的右上角的红色椭圆形标记,里面显示的数字表示须要用户处理的通知的数量。一样地,标记的颜色和形状、大小也是不能够更改的。App Store中有更新的应用程序的数量,Mail中收到的未读邮件的数量都是用标记通知用户的。

5.4. 声音(Sound)

声音提示也是iOS的一种通知方式,支持自定义,能够与前面三种通知类型搭配使用。

6.本地通知和推送通知

iOS应用程序会使用本地通知或推送通知来提醒用户:

6.1. 本地通知

应用程序的本地通知是由用户的iOS设备生成并发布的,不管这个应用程序是否运行在前台。就像一个日历应用,或者是一个to-do list应用,能够发出一条本地通知提醒用户立刻有一个会议要开始了。

6.2. 推送通知

通常会常用信鸽推送http://xg.qq.com/xg极光推送https://www.jpush.cn/两种推送第三方,应用程序的推送通知是这样发布的:该应用的远程服务器(Provider)先发出一条通知给苹果的推送通知服务(Apple Push Notification service, APNS),苹果的通知服务器再把这个通知推送给全部安装了这个应用的iOS设备。

若是要使用推送通知,必须先在苹果的推送通知服务里注册你要使用哪几种类型的通知,就好比下面的一段代码就表示同时注册了标记和声音两种类型的通知:

- (void)applicationDidFinishLaunching:(UIApplication *)app {// other setup tasks here….

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];}

如何使用各类通知

使用本地通知的应用程序可使用横幅、提醒、标记、声音,可是使用推送通知的应用程序须要先注册要使用的通知类型。

在使用通知的时候须要注意如下几点:

1. 保证标记的内容是最新的

2. 对同一事件不要发出多个通知

3. 通知内容不用包含应用程序的名称

对于标记型通知,当全部的事项都解决后,标记会自动消失。

在横幅和提醒中,以及顶部的通知中内心,iOS系统会自动在消息里显示应用程序的名称,因此在设计通知的内容时,就无需包含app的名称了。

在设计通知的内容时,还须要注意:

1. 关注于信息的表达,而不是用户的动做。避免提示用户去点哪个按钮或者是怎样打开app

2. 简短,最好不超过两行。长信息难以快速阅读,并且必然会有滚动条

3. 使用句式大写(sentence-style capitalization,第一个单词的首字母大写)和合适的标点符号,结尾通常使用句号

关于“提醒”

一条提醒可能会包含一到两个按钮。对于有两个按钮的提醒,须要把关闭提醒的按钮放在左边,把action button放在右边。

 

点左边的按钮会关闭提醒,点右边的按钮会关闭这条提醒并打开应用。

若是只有一个按钮,这个按钮应该是一个肯定按钮。

 

点击这个肯定按钮也只会关闭提醒,而不会打开应用。

对于提醒通知,在设计action button的title的时候须要注意:

1. title能准确描述打开应用的动做。好比点击Play按钮,就可以打开这个游戏,用户可以马上玩

2. 使用标题式大写(title-style capitalization,每一个单词的首字母都大写)

3. 足够简短

相关文章
相关标签/搜索