iOS实现本地通知
2011-04-13 12:22
447 查看
在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.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
通知的具体实现:
#pragma mark –
#pragma mark onChageValue
-(IBAction)onChangeValue:(id)sender
{
UISwitch *switch1=(UISwitch *)sender;
if (switch1.on) {
UILocalNotification *notification=[[UILocalNotification alloc] init];
NSDate *now1=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=NSDayCalendarUnit;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = NSLocalizedString(@"显示", nil);
switch (switch1.tag) {
case 0:
{
notification.fireDate=[now1 dateByAddingTimeInterval:10];
notification.alertBody=self.myLable1.text;
}
break;
case 1:
{
notification.fireDate=[now1 dateByAddingTimeInterval:20];
notification.alertBody=self.myLable2.text;
}
break;
case 2:
{
notification.fireDate=[now1 dateByAddingTimeInterval:30];
notification.alertBody=self.myLable3.text;
}
break;
default:
break;
}
[notification setSoundName:UILocalNotificationDefaultSoundName];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}else {
NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[myArray count]; i++) {
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) {
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}
}
}
}
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.localnotification/
开启通知。
通知的内容。
通知的次数。
下面是具体的实现:
首先是通知次数取消:
- (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.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
通知的具体实现:
#pragma mark –
#pragma mark onChageValue
-(IBAction)onChangeValue:(id)sender
{
UISwitch *switch1=(UISwitch *)sender;
if (switch1.on) {
UILocalNotification *notification=[[UILocalNotification alloc] init];
NSDate *now1=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=NSDayCalendarUnit;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = NSLocalizedString(@"显示", nil);
switch (switch1.tag) {
case 0:
{
notification.fireDate=[now1 dateByAddingTimeInterval:10];
notification.alertBody=self.myLable1.text;
}
break;
case 1:
{
notification.fireDate=[now1 dateByAddingTimeInterval:20];
notification.alertBody=self.myLable2.text;
}
break;
case 2:
{
notification.fireDate=[now1 dateByAddingTimeInterval:30];
notification.alertBody=self.myLable3.text;
}
break;
default:
break;
}
[notification setSoundName:UILocalNotificationDefaultSoundName];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}else {
NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[myArray count]; i++) {
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) {
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}
}
}
}
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.localnotification/
相关文章推荐
- IOS实现本地通知
- (七十三)iOS本地推送通知的实现
- iOSAPP本地通知的实现
- iOS开发中UILocalNotification实现本地通知实现提醒功能
- ios本地通知和远程通知详解-------(注:文章最后有个php服务端实现DEMO)
- ios-实现本地通知(UILocalNotification)
- ios本地通知和远程通知(文章最后有个php服务端实现DEMO)
- iOS实现本地通知(local notification)的方法
- Ios开发中UILocalNotification实现本地通知实现提醒功能
- Ios开发中UILocalNotification实现本地通知实现提醒功能
- iOS实现本地通知(local notification)的方法
- iOS项目开发实战——实现苹果本地消息通知推送服务
- iOS开发 UILocalNotification 本地通知实现提醒功能
- iOS实现本地通知
- iOS后台下载图片并实现本地通知(Swift)
- iOS本地通知的实现
- IOS实现本地通知
- 如何在 iOS 8 中使用 Swift 实现本地通知(上)
- iOS实现本地通知
- iOS开发 UILocalNotification 本地通知实现提醒功能