本地消息提示,ios使用UILocalNotification实现,下面简单举例:
UILocalNotification *notification = [[UILocalNotification alloc]init];
if (notification) {
//设置推送时间
NSDate *fireDate = [NSDate date];
notification.fireDate = fireDate;
//设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
//设置重复间隔
//notification.repeatInterval = NSWeekCalendarUnit;
//推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
//内容
notification.alertBody = @"我是分割线";
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = 1;
//notification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSMutableDictionary *dicNotificationInfo = [NSMutableDictionary dictionaryWithCapacity:0];
[dicNotificationInfo setObject:@"111" forKey:@"111"];
notification.userInfo = dicNotificationInfo;
//添加推送到uiapplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
当系统到了fireDate的时间就会抛送本地消息通知,这样点击消息提示,如果程序进入后台,就会触发本地消息监听函数,在AppDelegate中实现如下函数:
#pragma mark - 本地消息通知监听
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// 添加程序的处理代码
if (notification) {
NSLog(@"didFinishLaunchingWithOptions");
NSDictionary *userInfo = notification.userInfo;
......
}
}
如果程序被结束了,就会触发
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 程序正常启动所需要做的事情abc...
// 进行消息处理
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSDictionary *userInfo = notification.userInfo;
......
}
}
以上,完成本地消息的抛送及后续的处理,欢迎拍砖。