1、第一步先注册推送
在AppDelegate.m编写一个推送注册方法如下:
- (void)registerLocalPush
{
UILocalNotification *localNotifi = [UILocalNotification new];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotifi];
/*
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being
*/
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
2、在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数中调用如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self registerLocalPush];
return YES;
}
3、设置推送时间
- (void)pushLocalNotifi
{
// 1.创建通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// 2.设置通知的必选参数
// 设置通知显示的内容
localNotification.alertBody = @"推送显示的信息";
// 设置通知的发送时间,单位秒,在多少秒之后推送
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
//解锁滑动时的事件
localNotification.alertAction = @"XXOOXX";
//收到通知时App icon的角标
localNotification.applicationIconBadgeNumber = 0;
//推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName
localNotification.soundName = UILocalNotificationDefaultSoundName;
//设置推送自定义声音格式
//localNotification.soundName = @"文件名.扩展名";
//循环次数
localNotification.repeatInterval = kCFCalendarUnitDay;
// 3.发送通知(根据项目需要使用)
// 方式一: 根据通知的发送时间(fireDate)发送通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)clickLocalPush:(id)sender
{
[self pushLocalNotifi];
}