UILocalNotification的使用

闹钟  

iso4支持多任务的  可以使用local notification 不过时间到了 也只是出现个对话框 


1、“如果程序是启动的,可以通过 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
里面去实现一个alertview 
2、如果程序后台运行,就没有法去改变显示的alertview了,只能用系统的弹框了。 



@interface UIApplication (UILocalNotifications)

- (void)presentLocalNotificationNow:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);

- (void)scheduleLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);  // copies notification

- (void)cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);

- (void)cancelAllLocalNotifications NS_AVAILABLE_IOS(4_0);

@property(nonatomic,copy) NSArray *scheduledLocalNotifications NS_AVAILABLE_IOS(4_0);         // setter added in iOS 4.2

@end



NS_CLASS_AVAILABLE_IOS(4_0) @interface UILocalNotification : NSObject<NSCopying, NSCoding>       // added in iOS 4.0


// scheduling

@property(nonatomic,copy) NSDate *fireDate;

// the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer).

// pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock).

@property(nonatomic,copy) NSTimeZone *timeZone;


@property(nonatomic) NSCalendarUnit repeatInterval;      // 0 means don't repeat

@property(nonatomic,copy) NSCalendar *repeatCalendar;


// alerts

@property(nonatomic,copy) NSString *alertBody;      // defaults to nil. pass a string or localized string key to show an alert

@property(nonatomic) BOOL hasAction;                // defaults to YES. pass NO to hide launching button/slider

@property(nonatomic,copy) NSString *alertAction;    // used in UIAlert button or 'slide to unlock...' slider in place of unlock

@property(nonatomic,copy) NSString *alertLaunchImage;   // used as the launch image (UILaunchImageFile) when launch button is tapped


// sound

@property(nonatomic,copy) NSString *soundName;      // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName


// badge

@property(nonatomic) NSInteger applicationIconBadgeNumber;  // 0 means no change. defaults to 0


// user info

@property(nonatomic,copy) NSDictionary *userInfo;   // throws if contains non-property list types


@end



UIKIT_EXTERN NSString *const UILocalNotificationDefaultSoundName NS_AVAILABLE_IOS(4_0);



iOS 4.0之后,我们就多了一种推送选择,即本地推送,可以减轻一些服务器的压力。今天和大家简单分享下iOS4里的本地推送。


1、增加一个本地推送
//设置20秒之后 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];

    //chuagjian一个本地推送

    UILocalNotification *noti = [[[UILocalNotification allocinitautorelease];

    if (noti) {

        //设置推送时间

        noti.fireDate = date;

        //设置时区

        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];  

    }


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

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

{

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

message:notification.alertBody

   delegate:nil

  cancelButtonTitle:@"确定"

  otherButtonTitles:nil];

[alert show];

//这里,你就可以通过notification的useinfo,干一些你想做的事情了

application.applicationIconBadgeNumber -= 1;

}


3、取消一个本地推送

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 allocinit];

        }

        

        if (localNoti && !state) {

            //不推送 取消推送

            [app cancelLocalNotification:localNoti];

            [localNoti release];

            return;

        }

}


====================================================

iOS下的Notification的使用

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

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


UILocalNotification的调度

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

UILocalNotification的提醒内容

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

UILocalNotification提醒是需要的内容。alertBody是一串现实提醒内容的字符串(NSString),如果alertBody未设置的话,Notification被激发时将不现实提醒。alertAction也是一串字符 (NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为“View”相对文字 形式。alertLaunchImage是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图 片。hasAction是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。

UILocalNotification的其他部分

applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完 整。applicationIconBadgeNumber是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的Notification。soundName是另一个UILocalNotification用来提醒用户的手段,在Notification被激发之 后将播放这段声音来提醒用户有Notification需要处理,如果不设soundName的话,Notification被激发是将不会有声音播放, 除去应用特制的声音以外,也可以将soundName设为UILocalNotificationDefaultSoundName来使用系统默认提醒声 音。userInfo是Notification用来传递数据的NSDictionary。

登记UILocalNotification

在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。登记UILocalNotification * localNotification的方式为:

   [[UIApplication sharedApplication]  scheduleLocalNotification:localNotification];

在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification:

   [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

处理UILocalNotification

在提醒框动作按钮被点击后,应用开始运行时,可以在-(BOOL)application:didFinishLaunchingWithOptions:这个Application  delegate方法中处理。可以通过以下方式来加载为最近未处理的Notification:

   UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

如果应用正在运行时,可以通过覆盖在Application  Delegate中的方法-(void)application:didReceiveLocalNotification:来处理Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。

取消UILocalNotification

可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消

   [[UIApplication sharedApplication] cancelLocalNotification:localNotification];

   [[UIApplication sharedApplication] cancelAllLocalNotification];

总结

本地Notification的机制在应用开发中非常有效,可以很好的帮助开发者管理一些指定时间需要发生的事件,例如闹钟类的应用。而且因为系统统一对Notification的管理,让同样的任务可以非常简单得被处理,而无需让应用浪费资源去等待事件的触发。

========================================================================

//发送通知

    UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
NSDate *now=[NSDate new];
notification.fireDate=[now dateByAddingTimeInterval:10];//10秒后通知
notification.repeatInterval=0;//循环次数,kCFCalendarUnitWeekday一周一次
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.applicationIconBadgeNumber=1; //应用的红色数字
notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf"
//去掉下面2行就不会弹出提示框
notification.alertBody=@"通知内容";//提示信息 弹出提示框
notification.alertAction = @"打开";  //提示框按钮
//notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失//  NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
//notification.userInfo = infoDict; //添加额外的信息

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

UILocalNotification需要4.0版本支持!!

应用程序启动后,当10秒钟过去后,如果应用还是在前台运行,而且应用委托实现- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法,则会进入该函数;

如果10秒过后,应用在后台或,则会弹出alert框提示用户;

注意: 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。

//取消通知

复制代码

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

// Override point for customization after application launch.
application.applicationIconBadgeNumber = 0;
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//点击提示框的打开
application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//当程序还在后天运行
application.applicationIconBadgeNumber = 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值