@@ -36,25 +36,6 @@ @interface DPHostNotificationProfile ()
3636// / 通知に関する情報を管理するオブジェクト
3737@property NSMutableDictionary *notificationInfoDict;
3838
39- /* !
40- OnClickイベントメッセージを送信する
41- @param notificationId イベントが発生した通知の通知ID
42- @param serviceId サービスID
43- */
44- - (void ) sendOnClickEventWithNotificaitonId : (NSString *)notificationId serviceId : (NSString *)serviceId ;
45- /* !
46- OnShowイベントメッセージを送信する
47- @param notificationId イベントが発生した通知の通知ID
48- @param serviceId サービスID
49- */
50- - (void ) sendOnShowEventWithNotificaitonId : (NSString *)notificationId serviceId : (NSString *)serviceId ;
51- /* !
52- OnCloseイベントメッセージを送信する
53- @param notificationId イベントが発生した通知の通知ID
54- @param serviceId サービスID
55- */
56- - (void ) sendOnCloseEventWithNotificaitonId : (NSString *)notificationId serviceId : (NSString *)serviceId ;
57-
5839@end
5940
6041@implementation DPHostNotificationProfile
@@ -71,27 +52,18 @@ - (instancetype)init
7152 _notificationInfoDict = @{}.mutableCopy ;
7253
7354 [self setNotificationIdLength: 3 ];
74-
75- dispatch_async (dispatch_get_main_queue (), ^{
76- [[NSNotificationCenter defaultCenter ] addObserver: weakSelf
77- selector: @selector (didReceiveLocalNotification: )
78- name: @" UIApplicationDidReceiveLocalNotification"
79- object: nil ];
80- });
55+
56+ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter ];
57+ center.delegate = self;
8158
8259 // API登録(didReceivePostNotifyRequest相当)
8360 NSString *postNotifyRequestApiPath = [self apiPath: nil
8461 attributeName: DConnectNotificationProfileAttrNotify];
8562 [self addPostPath: postNotifyRequestApiPath
8663 api: ^BOOL (DConnectRequestMessage *request, DConnectResponseMessage *response) {
87-
88- NSData *icon = [DConnectNotificationProfile iconFromRequest: request];
64+
8965 NSNumber *type = [DConnectNotificationProfile typeFromRequest: request];
90- NSString *dir = [DConnectNotificationProfile dirFromRequest: request];
91- NSString *lang = [DConnectNotificationProfile langFromRequest: request];
9266 NSString *body = [DConnectNotificationProfile bodyFromRequest: request];
93- NSString *tag = [DConnectNotificationProfile tagFromRequest: request];
94- NSString *serviceId = [request serviceId ];
9567
9668 if (!body) {
9769 [response setError: 100 message: @" body is nill" ];
@@ -105,7 +77,6 @@ - (instancetype)init
10577 do {
10678 notificationId = [DPHostUtils randomStringWithLength: [weakSelf NotificationIdLength ]];
10779 } while ([weakSelf notificationInfoDict ][notificationId]);
108- UILocalNotification *notification;
10980 NSString *status = @" EVENT \n " ;
11081 switch ([type intValue ]) {
11182 case DConnectNotificationProfileNotificationTypePhone:
@@ -124,23 +95,31 @@ - (instancetype)init
12495 [response setErrorToInvalidRequestParameterWithMessage: @" Not support type" ];
12596 return YES ;
12697 }
127- notification = [self scheduleWithAlertBody: [status stringByAppendingString: body]
128- userInfo: @{@" id" :notificationId,@" serviceId" :serviceId}];
98+
99+ UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc ] init ];
100+ content.title = [NSString localizedUserNotificationStringForKey: status
101+ arguments: nil ];
102+ content.body = [NSString localizedUserNotificationStringForKey: body
103+ arguments: nil ];
104+ content.subtitle = [NSString localizedUserNotificationStringForKey: [status stringByAppendingString: body]
105+ arguments: nil ];
106+ content.sound = [UNNotificationSound defaultSound ];
107+ // Deliver the notification in five seconds.
108+ UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger
109+ triggerWithTimeInterval: 1
110+ repeats: NO ];
111+ UNNotificationRequest * nRequest = [UNNotificationRequest requestWithIdentifier: notificationId
112+ content: content
113+ trigger: trigger];
129114
130- // 通知情報を生成し、notificationInfoDictにて管理
131- NSMutableArray *notificationInfo =
132- @[type,
133- dir ? dir : [NSNull null ],
134- lang ? lang : [NSNull null ],
135- body ? body : [NSNull null ],
136- tag ? tag : [NSNull null ],
137- icon ? icon : [NSNull null ],
138- notification,
139- serviceId ].mutableCopy ;
140- [weakSelf notificationInfoDict ][notificationId] = notificationInfo;
141- [weakSelf sendOnShowEventWithNotificaitonId: notificationId
142- serviceId: [weakSelf notificationInfoDict ][notificationId][NotificationIndexServiceId]];
143- [response setString: notificationId forKey: DConnectNotificationProfileParamNotificationId];
115+ // Schedule the notification.
116+ UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter ];
117+
118+ [center addNotificationRequest: nRequest
119+ withCompletionHandler: ^(NSError * _Nullable error) {
120+ }];
121+ [weakSelf sendOnShowEventWithNotificaitonId: nRequest.identifier];
122+ [response setString: nRequest.identifier forKey: DConnectNotificationProfileParamNotificationId];
144123 [response setResult: DConnectMessageResultTypeOk];
145124
146125 return YES ;
@@ -217,27 +196,30 @@ - (instancetype)init
217196 attributeName: DConnectNotificationProfileAttrNotify];
218197 [self addDeletePath: deleteNotifyRequestApiPath
219198 api: ^BOOL (DConnectRequestMessage *request, DConnectResponseMessage *response) {
220-
221- NSString *serviceId = [request serviceId ];
222199 NSString *notificationId = [DConnectNotificationProfile notificationIdFromRequest: request];
223200
224201 if (!notificationId) {
225202 [response setErrorToInvalidRequestParameterWithMessage: @" notificationId must be specified." ];
226203 return YES ;
227204 }
228- if (![weakSelf notificationInfoDict ][notificationId]) {
205+ UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter ];
206+ [center getDeliveredNotificationsWithCompletionHandler: ^(NSArray <UNNotification *> * _Nonnull notifications) {
207+ for (UNNotification *notification in notifications) {
208+ NSString *currentId = notification.request .identifier ;
209+ if ([currentId isEqualToString: notificationId]) {
210+ [center removeDeliveredNotificationsWithIdentifiers: @[notificationId]];
211+ [weakSelf sendOnCloseEventWithNotificaitonId: notificationId];
212+ [[weakSelf notificationInfoDict ] removeObjectForKey: notificationId];
213+ [response setResult: DConnectMessageResultTypeOk];
214+ [[DConnectManager sharedManager ] sendResponse: response];
215+ return ;
216+ }
217+ }
229218 [response setErrorToInvalidRequestParameterWithMessage: @" Specified notificationId does not exist." ];
230- return YES ;
231- }
232-
219+ [[DConnectManager sharedManager ] sendResponse: response];
220+ }];
233221
234- UILocalNotification *notification = [weakSelf notificationInfoDict ][notificationId][NotificationIndexNotifiation];
235- [[UIApplication sharedApplication ] cancelLocalNotification: notification];
236- [weakSelf sendOnCloseEventWithNotificaitonId: notificationId serviceId: serviceId];
237- [[weakSelf notificationInfoDict ] removeObjectForKey: notificationId];
238- [response setResult: DConnectMessageResultTypeOk];
239-
240- return YES ;
222+ return NO ;
241223 }];
242224
243225 // API登録(didReceiveDeleteOnClickRequest相当)
@@ -310,22 +292,8 @@ - (instancetype)init
310292}
311293
312294
313- -(void )didReceiveLocalNotification : (NSNotification *)notification {
314- NSDictionary *userInfo = [notification userInfo ];
315- if (userInfo) {
316- [self sendOnClickEventWithNotificaitonId: userInfo[@" id" ] serviceId: userInfo[@" serviceId" ]];
317- }
318- }
319-
320- - (void )dealloc {
321- [[NSNotificationCenter defaultCenter ] removeObserver: self
322- name: @" UIApplicationDidReceiveLocalNotification"
323- object: nil ];
324- }
325-
326-
327-
328- - (void ) sendOnClickEventWithNotificaitonId : (NSString *)notificationId serviceId : (NSString *)serviceId
295+ #pragma mark - Send Event
296+ - (void ) sendOnClickEventWithNotificaitonId : (NSString *)notificationId
329297{
330298 // イベントの取得
331299 NSArray *evts = [_eventMgr eventListForServiceId: DPHostDevicePluginServiceId
@@ -340,7 +308,7 @@ - (void) sendOnClickEventWithNotificaitonId:(NSString *)notificationId serviceId
340308 }
341309}
342310
343- - (void ) sendOnShowEventWithNotificaitonId : (NSString *)notificationId serviceId : ( NSString *) serviceId
311+ - (void ) sendOnShowEventWithNotificaitonId : (NSString *)notificationId
344312{
345313 // イベントの取得
346314 NSArray *evts = [_eventMgr eventListForServiceId: DPHostDevicePluginServiceId
@@ -355,7 +323,7 @@ - (void) sendOnShowEventWithNotificaitonId:(NSString *)notificationId serviceId:
355323 }
356324}
357325
358- - (void ) sendOnCloseEventWithNotificaitonId : (NSString *)notificationId serviceId : ( NSString *) serviceId
326+ - (void ) sendOnCloseEventWithNotificaitonId : (NSString *)notificationId
359327{
360328 // イベントの取得
361329 NSArray *evts = [_eventMgr eventListForServiceId: DPHostDevicePluginServiceId
@@ -370,19 +338,20 @@ - (void) sendOnCloseEventWithNotificaitonId:(NSString *)notificationId serviceId
370338 }
371339}
372340
373- #pragma mark - private method
374-
375341
376- // ローカル通知を作成する
377- - (UILocalNotification *)scheduleWithAlertBody : (NSString *)alertBody userInfo : (NSDictionary *) userInfo {
378- UILocalNotification *notification = [[UILocalNotification alloc ] init ];
379- [notification setUserInfo: userInfo];
380- [notification setTimeZone: [NSTimeZone systemTimeZone ]];
381- [notification setAlertBody: alertBody];
382- [notification setUserInfo: userInfo];
383- [notification setAlertAction: @" Open" ];
384- [[UIApplication sharedApplication ] scheduleLocalNotification: notification];
385- return notification;
342+ #pragma mark - Notification Delegate
343+ - (void )userNotificationCenter : (UNUserNotificationCenter *)center
344+ didReceiveNotificationResponse : (UNNotificationResponse *)response
345+ withCompletionHandler : (void (^)())completionHandler {
346+ completionHandler ();
347+ [self sendOnClickEventWithNotificaitonId: response.notification.request.identifier];
386348}
387349
350+ - (void )userNotificationCenter : (UNUserNotificationCenter *)center
351+ willPresentNotification : (UNNotification *)notification
352+ withCompletionHandler : (void (^)(UNNotificationPresentationOptions ))completionHandler {
353+ completionHandler (UNNotificationPresentationOptionBadge |
354+ UNNotificationPresentationOptionSound |
355+ UNNotificationPresentationOptionAlert );
356+ };
388357@end
0 commit comments