forkdog

记录学习的地方

0%

iOS NSNotification本地通知

iOS10 废弃了 UILocalNotification,采用了新的UserNotifications Framework来推送通知。

注册推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if (@available(iOS 10.0, *)) {
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
//center.delegate = self;

//iOS 10 使用以下方法注册,才能得到授权,注册通知以后,会自动注册 deviceToken,如果获取不到 deviceToken,Xcode8下要注意开启 Capability->Push Notification。
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {

}];

//获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

}];
}

return YES;
}

@end

使用本地通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

- (void)showLocalNotificationWithTitle:(NSString *)title notificationId:(NSString *)notificationId {

if (@available(iOS 10.0, *)) {

//新增前先清楚已注册的相同ID的本地推送
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removePendingNotificationRequestsWithIdentifiers:@[notificationId]];

//创建一个通知内容对象
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:title arguments:nil];
//content.body = [NSString localizedUserNotificationStringForKey:@"消息" arguments:nil];
// content.sound = [UNNotificationSound defaultSound];
content.userInfo = @{kClickLocalNotification: notificationId};

//设置n秒后推送
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:notificationId content:content trigger:trigger];

//添加
[center addNotificationRequest:request withCompletionHandler:nil];
}
else {

//新增前先清楚已注册的相同ID的本地推送
[self deleteLocadNotificationWithNotificationId:notificationId];

UILocalNotification *notification = [[UILocalNotification alloc] init];
//触发通知的时间
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
//时区
notification.timeZone = [NSTimeZone defaultTimeZone];

//重复次数 kCFCalendarUnitEra为不重复
notification.repeatInterval = kCFCalendarUnitEra;

//通知内容
notification.alertBody = title;
notification.applicationIconBadgeNumber = 0;
//通知声音
notification.soundName = UILocalNotificationDefaultSoundName;
//通知dict
notification.userInfo = @{kClickLocalNotification: notificationId};

// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge; //不使用UIUserNotificationTypeSound
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

} else {

}

//执行通知注册
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}


- (void)deleteLocadNotificationWithNotificationId:(NSString *)notificationId {

if (@available(iOS 10.0, *)) {

/*
removePendingNotificationRequestsWithIdentifiers 删除特定等待递送的通知
removeDeliveredNotificationsWithIdentifiers //删除特定已经递送的通知
*/
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]];
}
else {
UILocalNotification * notification = [self queryNotificationWithNotificatioId:notificationId];

if (notification) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}


//查询符合条件的本地推送
- (UILocalNotification *)queryNotificationWithNotificatioId:(NSString *)notificatioId {
NSArray * notifications = [self queryAllSystemNotifications];
__block UILocalNotification * localnotification = nil;

if (notifications.count > 0) {
[notifications enumerateObjectsUsingBlock:^(UILocalNotification * obj, NSUInteger idx, BOOL * _Nonnull stop) {
//查找符合条件的本地推送
if ([obj.userInfo containsObjectForKey:kClickLocalNotification] && [obj.userInfo[kClickLocalNotification] isEqualToString:notificatioId]) {
localnotification = obj;
*stop = YES;
}
}];
}
return localnotification;
}


//查询所有已注册的本地推送
- (NSArray *)queryAllSystemNotifications {
return [[UIApplication sharedApplication] scheduledLocalNotifications];
}
1
2
getPendingNotificationRequestsWithCompletionHandler:  //获取所有等待递送的通知
getDeliveredNotificationsWithCompletionHandler: //获取所有已经递送的通知