1.首先新建一个class,继承UIViewController。取名叫MyViewController
2.直接在MyViewController.m中填写
//
// MyViewController.m
// myLocalNotice
//
// Created by mac on 14-8-1.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import "MyViewController.h"
@implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20, 20, 280, 20);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:@"测试通知" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(createNotice) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)createNotice{
NSLog(@"此处创建通知!!!");
<span style="color:#ff0000;"> //核心</span>
<span style="color:#ff0000;">UILocalNotification *notification = [[UILocalNotification alloc] init];
//设置10秒之后
//NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:20];
if (notification != nil) {
NSDate *now=[NSDate date];
// 设置推送时间
notification.fireDate = [now dateByAddingTimeInterval:30];
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
notification.alertBody = @"您有新的消息请注意查收!";
// 锁屏后提示文字,一般来说,都会设置与alertBody一样
notification.alertAction=NSLocalizedString(@"屏幕已经锁了", nil);
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
//给这个通知增加key 便于半路取消。nfkey这个key是自己随便写的,还有notificationtag也是自己定义的ID。假如你的通知不会在还没到时间的时候手动取消,那下面的两行代码你可以不用写了。取消通知的时候判断key和ID相同的就是同一个通知了。
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1],@"nfkey",nil];
[notification setUserInfo:dict];
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
</span>
}
}
@end
AppDelegate.m的文件中代码如下
//
// AppDelegate.m
// myLocalNotice
//
// Created by mac on 14-8-1.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "AppDelegate.h"
#import "MyViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController=[[MyViewController alloc] init];
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
<span style="color:#ff0000;">//app在前台运行,通知时间到了,调用的方法</span>
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (notification)
{
application.applicationIconBadgeNumber=0;
int count =[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
if(count>0)
{
NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0];
for (int i=0; i<count; i++)
{
UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
notif.applicationIconBadgeNumber=i+1;
[newarry addObject:notif];
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (newarry.count>0)
{
for (int i=0; i<newarry.count; i++)
{
UILocalNotification *notif = [newarry objectAtIndex:i];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
}
}
}
//<span style="background-color: rgb(255, 102, 102);">app在后台运行,通知时间到了,你从通知栏进入,或者直接点app图标进入时,会走的方法。</span>
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
int count =[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
if(count>0)
{
NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0];
for (int i=0; i<count; i++)
{
UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
notif.applicationIconBadgeNumber=i+1;
[newarry addObject:notif];
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (newarry.count>0)
{
for (int i=0; i<newarry.count; i++)
{
UILocalNotification *notif = [newarry objectAtIndex:i];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
}
//删除通知
NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications];
NSUInteger acount=[narry count];
if (acount>0)
{
// 遍历找到对应nfkey和notificationtag的通知
for (int i=0; i<acount; i++)
{
UILocalNotification *myUILocalNotification = [narry objectAtIndex:i];
NSDictionary *userInfo = myUILocalNotification.userInfo;
NSNumber *obj = [userInfo objectForKey:@"nfkey"];
int mytag=[obj intValue];
if (mytag==1)
{
<span style="white-space:pre"> </span>//关键点......
NSLog(@"cancel all notification!!");
// 删除本地通知
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
//重置通知为0
application.applicationIconBadgeNumber=0;
// break;
}
}
}
}
@end