通过UILocalNotification 实现本地通知的推送
1 创建通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.alertTitle = @"打开广播";
notification.alertBody = @"广播";
notification.repeatInterval = NSCalendarUnitSecond;
//设置本地通知的时区
notification.timeZone = [NSTimeZone defaultTimeZone];
//设置角标的个数
notification.applicationIconBadgeNumber=1;
notification.userInfo = @{@"info":@"broadcast"};
notification.soundName = UILocalNotificationDefaultSoundName;
2 取消通知
UIApplication *app = [UIApplication sharedApplication];
NSArray *array = [app scheduledLocalNotifications];
for (UILocalNotification * local in array) {
NSDictionary *dic = local.userInfo;
if ([dic[@"info"] isEqual: @"broadcast"]) {
//删除指定的通知
[app cancelLocalNotification:local];
}
}
//也可以使用[app cancelAllLocalNotifications]删除所有通知;
3 app跳出到后台时,可在下列方法内添加通知提示
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
4 app处于运行阶段时
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//判断应用程序当前的运行状态是否为激活状态
if (application.applicationState == UIApplicationStateActive) {
//只有激活状态 在app运行时本地通知才有效
}
}