您的位置:首页 > 产品设计 > UI/UE

关于UILocalNotification一些更深刻的认识

2013-08-22 23:11 393 查看
不费话,直接上代码,然后解释

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];

notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;

NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}


取消所有已经设置的本地通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];


代码兼容,确保本地通知可用。因为本地通知是iOS4以后的故事,所以直接在较早的系统上用的话会崩溃~

Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];


初始化一个本地通知

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

设置本地通知的时间和时区.话说,我一直以为fireDate是firstDate呢。

notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];


控制弹框内容和动作按钮标题

notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";






这个alertAction也会影响”划动解锁”,就是锁屏的时候收到本地通知的显示





设置提醒声音
notif.soundName = UILocalNotificationDefaultSoundName;


上面是使用系统默认的铃声,你也可以指定一下.

notif.soundName = @"sms.caf";


需要注意的sms.caf有时间长度和格式限制

设置程序Icon上的数字
notif.applicationIconBadgeNumber = 1;


针对本地通知设置一些自定义信息:

NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;


就是方便传递一些数据的

计划这个本地通知,并释放。
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];


关于计划通知还一个更酷的功能:立即显示

[[UIApplication sharedApplication] presentLocalNotificationNow:noti];


ok,解释完了,上面这些东西,几乎所以关于本地通知的教程都有,我要说的是下面的。

处理好本地通知:

1.应用未运行的情况

2.应用正在运行。

3.应用运行在后台(挂起或者在执行后台任务)

应用未运行的情况

这时候只要处理好,程序启动时的参数launchOptions就可以了。

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];

if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
}

application.applicationIconBadgeNumber = 0;

[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}


应用运行在后台

这时程序会优先通过本地代理获取这个通知。

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

application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
NSLog(@"%@",reminderText);
}


应用运行在后台

应用启动完以后,用户按了一下Home键,应用就被搁到后台运行了。这时当有一个通知,用户点击”View”的时间,你会发现上面一个代理还是会被调用。区别这个通知有没有被用户看到过也很简单,加个应用运行状态判断就好了。

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

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
//这个通知用户已经看过了。
}


转载地址:http://blog.cnrainbird.com/index.php/2012/06/08/guan_yu_uilocalnotification_yi_xie_geng_shen_ke_de_ren_shi/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: