这几天在实现一个日程的功能,其中包含通知推送。搞通知推送搞了一天,目前依然还有个问题(角标的问题)待解决。
首先,贴上一段创建通知的代码:
- (void)createLocalNotificationByValue: (NSString *)value key: (NSString *)key andTime: (NSDate *)time {
// 创建一个本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
//设置10秒之后
//NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:60];
if (notification != nil) {
// 设置推送时间
notification.fireDate = time;
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
notification.alertBody = value;
//写入通知到plist
[self createNotificationCountIfNeeded];
//[self writeNotificationToPlist:value and:key];
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = [self writeNotificationToPlist:value and:key];;
//设置userinfo 方便在之后需要撤销的时候使用
NSDictionary *info = [NSDictionary dictionaryWithObject:value forKey:key];
notification.userInfo = info;
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
}
创建的代码网上有,这段也是从网上找来,改了一些自己的东西,就不多说了。
———————————————————————————————————————————————————
然后,需要在三个地方(app的代理中)响应通知:
1、程序启动时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}
2、点击通知时
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
}
3、程序从后台回来前台时
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
———————————————————————————————————————————————————
移除通知的问题
不多说,直接贴上自己的代码:
//删除通知
- (int)removeNotificationByKey: (NSString *)key
{
NSString *path = [self notificationCountDirectory];
//NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[dict removeObjectForKey:key];
[dict writeToFile:path atomically:YES];
UILocalNotification *notification;
NSArray *localArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *localNotification in localArray) {
for (NSString *localKey in localNotification.userInfo) {
//若存在key的通知,进行删除
if ([localKey isEqualToString:key]) {
notification = localNotification;
}
}
}
if (notification == nil) {
notification = [[UILocalNotification alloc] init];
}
if (notification) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
return [dict count];
}
UILocalNotification对象有个userInfo的属性(NSDictionary类型), 故在创建通知时把每个通知的userInfo储存在plist文件中,以实现之后的通知响应及删除。
——————————————————————————————————————————————————
最后,已推送的通知的移除问题
//解除所有已推送的通知
- (void)removeNotifications
{
//获得通知信息的key,再通过此删除通知
NSArray *localArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
//获得通知信息的key,在通过此删除通知
for (UILocalNotification *localNotification in localArray) {
for (NSString *key in localNotification.userInfo) {
//若通知的时间不晚于当前时间,则解除通知
if (![localNotification.fireDate isEqualToDate:[localNotification.fireDate laterDate:[NSDate date]]]) {
[self removeNotificationByKey:key];
}
}
}
}
代码很简单,就是将通知的推送时间跟当前时间比较,若早于当前时间,就移除之
在这里,可能是自己还不够熟悉SDK,不知道有没有移除已推送通知的简便方法,所以只能这样做了
另外说明一下,这句
NSArray *localArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 是获取当前已注册通知的Array。
——————————————————————————————————————————————————
不怎么会写技术文章,就这么多了。
最最后,提一下自己还没解决的问题:
notification.applicationIconBadgeNumber 是通知推送时,app图标上角标的数字,即 值为1时,角标数字为1,值为2时,角标数字为2 。这里就有个问题了,它不是累加已有角标的,我不知道怎样让它累加。