iOS实例 实现本地通知

移动开发 iOS
本文介绍的是iOS实例 实现本地通知,介绍了通知的定义和最简单的实现,具体内容来看本文。

iOS实例 实现本地通知是本文要介绍的内容,在iOS实现本地通知这篇文章中,介绍了通知的定义和最简单的实现。下面我将介绍一个比较复杂一点的例子,实现的效果如下:

iOS实例 实现本地通知

开启通知。

iOS实例 实现本地通知

通知的内容。

iOS实例 实现本地通知

通知的次数。

下面是具体的实现:

首先是通知次数取消:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.       
  3.     // Override point for customization after application launch.   
  4.     /////////////   
  5.     application.applicationIconBadgeNumber = 0;   
  6.     // Add the view controller’s view to the window and display.   
  7.     [self.window addSubview:viewController.view];   
  8.     [self.window makeKeyAndVisible];  
  9.  
  10.     return YES;   
  11. }  

通知的具体实现:

  1. #pragma mark –   
  2. #pragma mark onChageValue   
  3. -(IBAction)onChangeValue:(id)sender   
  4. {   
  5.     UISwitch *switch1=(UISwitch *)sender;   
  6.     if (switch1.on) {   
  7.         UILocalNotification *notification=[[UILocalNotification alloc] init];   
  8.         NSDate *now1=[NSDate date];    
  9.         notification.timeZone=[NSTimeZone defaultTimeZone];   
  10.         notification.repeatInterval=NSDayCalendarUnit;   
  11.         notification.applicationIconBadgeNumber = 1;   
  12.         notification.alertAction = NSLocalizedString(@"显示", nil);   
  13.         switch (switch1.tag) {   
  14.             case 0:   
  15.             {   
  16.                 notification.fireDate=[now1 dateByAddingTimeInterval:10];   
  17.                 notification.alertBody=self.myLable1.text;   
  18.             }   
  19.                 break;   
  20.             case 1:   
  21.             {   
  22.                 notification.fireDate=[now1 dateByAddingTimeInterval:20];   
  23.                 notification.alertBody=self.myLable2.text;   
  24.             }   
  25.                 break;   
  26.             case 2:   
  27.             {   
  28.                 notification.fireDate=[now1 dateByAddingTimeInterval:30];   
  29.                 notification.alertBody=self.myLable3.text;   
  30.             }   
  31.                 break;   
  32.             default:   
  33.                 break;   
  34.         }   
  35.         [notification setSoundName:UILocalNotificationDefaultSoundName];   
  36.         NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:   
  37.                               [NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil];   
  38.         [notification setUserInfo:dict];   
  39.         [[UIApplication sharedApplication]   scheduleLocalNotification:notification];   
  40.     }else {   
  41.         NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];   
  42.         for (int i=0; i<[myArray count]; i++) {   
  43.             UILocalNotification    *myUILocalNotification=[myArray objectAtIndex:i];   
  44.             if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) {   
  45.                 [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];   
  46.             }   
  47.         }   
  48.     }   

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.localnotification/

小结:iOS实例 实现本地通知的内容介绍完了,希望本文对你有所帮助。

本文来自:http://wangjun.easymorse.com/?p=1239

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-22 16:57:44

iOS 通知

2011-07-22 18:20:04

IOS View 动画

2014-07-15 09:26:13

swiftiosMPGNotifica

2011-07-22 17:48:29

IOS Socket TCP

2015-03-09 09:44:35

GmailiOS

2019-05-07 16:15:06

iOS系统系统通知库通知消息

2022-07-01 17:14:03

消息通知鸿蒙

2014-08-04 14:19:00

iOS通知中心扩展

2011-05-04 14:40:57

推送通知iOS

2015-03-30 09:32:15

XcodeiOS应用程序

2011-05-31 16:05:41

WidgetiOS 5

2011-07-26 11:08:23

iOS 录像 录音

2016-08-11 10:43:56

2012-05-14 17:10:50

iOS

2012-05-13 13:15:54

IOS

2010-02-23 14:48:38

WCF事件通知

2015-08-14 10:32:10

CentOSOpenStack搭建本地源

2021-04-23 14:07:55

iOS 15iPhone界面

2011-07-22 16:37:14

IOS CALayer 动画

2016-11-18 12:53:36

iOS 10苹果
点赞
收藏

51CTO技术栈公众号