开发中不仅仅有远程推送还有本地通知,这样可以减轻服务器的压力。
一、在IOS8中需要先注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
return YES;
}
二、在-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法中接收通知
//这个方法只有在程序启动之后才会执行,因此当程序处于后台时,该方法不会执行
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@",notification);
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"通知" message:@"下班了" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertView show];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
三、创建本地通知
//
// ViewController.m
// UILocalNotification
//
// Created by City--Online on 15/5/15.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILocalNotification *notification=[[UILocalNotification alloc]init];
if (notification) {
//设置推送时间
NSDate *currentDate=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.fireDate=[currentDate dateByAddingTimeInterval:10];
//设置重复间隔 NSCalendarUnit类型 0表示不重复
notification.repeatInterval=kCFCalendarUnitMinute;
//设置提醒的文字
//设备收到本地通知时横额或锁屏时的主要文字内容
notification.alertBody=@"内容";
notification.alertTitle=@"标题";
//锁屏时显示的slide to后面的文字内容
notification.alertAction=NSLocalizedString(@"我是谁?", nil);
//提示音 默认
notification.soundName=UILocalNotificationDefaultSoundName;
//设置应用程序右上角的提醒个数
notification.applicationIconBadgeNumber++;
//设置通知的userInfo
NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init];
[userInfo setObject:@"kLocalNotificationID" forKey:@"kLocalNotificationID"];
[userInfo setObject:@"cuiyanwei" forKey:@"name"];
notification.userInfo=userInfo;
//将通知添加到系统中
//如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)(注册到系统服务里并不是在APP中)
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
// [self delete];
}
}
//删除本地通知
-(void)delete
{
//往系统服务里面注册之后有时会需要删除
//1.暴力删除,取消所有的通知
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
//2.删除制定的通知
for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
NSString *kLocalNotificationID=[notification.userInfo objectForKey:@"kLocalNotificationID"];
NSLog(@"%@",kLocalNotificationID);
if ([kLocalNotificationID isEqualToString:@"kLocalNotificationID"]) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"取消通知" message:@"已经取消通知" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:nil, nil];
[alertView show];
break;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
四、运行结果