Skip to content

Commit 388334f

Browse files
Merge pull request #150 from TakayukiHoshi1984/modify_host_notification
HostデバイスプラグインのNotificationProfileの更新
2 parents 4972ade + 072c762 commit 388334f

4 files changed

Lines changed: 71 additions & 95 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost.xcodeproj/project.pbxproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
97FE1A21192B6DA400808FA5 /* DPHostSystemProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = 97FE1A20192B6DA400808FA5 /* DPHostSystemProfile.m */; };
6363
AB3B2C281BCF311000C5E3A2 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB3B2C261BCF311000C5E3A2 /* AVFoundation.framework */; };
6464
AB3B2C291BCF311000C5E3A2 /* AVKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB3B2C271BCF311000C5E3A2 /* AVKit.framework */; };
65+
ABA43AC41EE562ED0019FE35 /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABA43AC31EE562ED0019FE35 /* UserNotifications.framework */; };
6566
ABDA3C6D1A0C58F3001F30A0 /* DPHostConnectionProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = ABDA3C6C1A0C58F3001F30A0 /* DPHostConnectionProfile.m */; };
6667
ABDA3C701A0C743A001F30A0 /* CoreBluetooth.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABDA3C6F1A0C743A001F30A0 /* CoreBluetooth.framework */; };
6768
D62F0D1A1AA6E2CB00BC0ADA /* close_small_button_focus.png in Resources */ = {isa = PBXBuildFile; fileRef = D62F0D171AA6E2CB00BC0ADA /* close_small_button_focus.png */; };
@@ -150,6 +151,7 @@
150151
97FE1A20192B6DA400808FA5 /* DPHostSystemProfile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DPHostSystemProfile.m; sourceTree = "<group>"; };
151152
AB3B2C261BCF311000C5E3A2 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
152153
AB3B2C271BCF311000C5E3A2 /* AVKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVKit.framework; path = System/Library/Frameworks/AVKit.framework; sourceTree = SDKROOT; };
154+
ABA43AC31EE562ED0019FE35 /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = System/Library/Frameworks/UserNotifications.framework; sourceTree = SDKROOT; };
153155
ABDA3C6B1A0C58F3001F30A0 /* DPHostConnectionProfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DPHostConnectionProfile.h; sourceTree = "<group>"; };
154156
ABDA3C6C1A0C58F3001F30A0 /* DPHostConnectionProfile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DPHostConnectionProfile.m; sourceTree = "<group>"; };
155157
ABDA3C6F1A0C743A001F30A0 /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = System/Library/Frameworks/CoreBluetooth.framework; sourceTree = SDKROOT; };
@@ -201,6 +203,7 @@
201203
isa = PBXFrameworksBuildPhase;
202204
buildActionMask = 2147483647;
203205
files = (
206+
ABA43AC41EE562ED0019FE35 /* UserNotifications.framework in Frameworks */,
204207
EF19A24F1E446BDD00F00439 /* CoreLocation.framework in Frameworks */,
205208
AB3B2C281BCF311000C5E3A2 /* AVFoundation.framework in Frameworks */,
206209
AB3B2C291BCF311000C5E3A2 /* AVKit.framework in Frameworks */,
@@ -290,6 +293,7 @@
290293
97FE19E9192B2FB400808FA5 /* Frameworks */ = {
291294
isa = PBXGroup;
292295
children = (
296+
ABA43AC31EE562ED0019FE35 /* UserNotifications.framework */,
293297
EF19A24E1E446BDD00F00439 /* CoreLocation.framework */,
294298
AB3B2C261BCF311000C5E3A2 /* AVFoundation.framework */,
295299
AB3B2C271BCF311000C5E3A2 /* AVKit.framework */,
@@ -371,7 +375,6 @@
371375
EF36CA041E383B56008E6F3E /* DPHostLightProfile.m */,
372376
EF19A2491E4467B200F00439 /* DPHostGeolocationProfile.h */,
373377
EF19A24A1E4467B200F00439 /* DPHostGeolocationProfile.m */,
374-
AB0B13221A80A07900D655D5 /* DPHostFileDescriptor */,
375378
D6384EA71A7625BD00384167 /* DPHostCanvasProfile.h */,
376379
D6384EA81A7625BD00384167 /* DPHostCanvasProfile.m */,
377380
D6384EAA1A7625CF00384167 /* DPHostCanvas */,

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost/Classes/DPHostService.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ - (instancetype) initWithFileManager: (DConnectFileManager *) fileMgr plugin: (i
4848
[self addProfile:[DPHostFileProfile new]];
4949
[self addProfile:[DPHostMediaPlayerProfile new]];
5050
[self addProfile:[DPHostMediaStreamRecordingProfile new]];
51-
[self addProfile:[DPHostNotificationProfile new]];
51+
float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
52+
if(iOSVersion >= 10.0) {
53+
[self addProfile:[DPHostNotificationProfile new]];
54+
}
5255
[self addProfile:[DPHostPhoneProfile new]];
5356
[self addProfile:[DPHostProximityProfile new]];
5457
[self addProfile:[DPHostSettingProfile new]];

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost/Classes/profile/DPHostNotificationProfile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//
99

1010
#import <DConnectSDK/DConnectSDK.h>
11+
#import <UserNotifications/UserNotifications.h>
1112

12-
@interface DPHostNotificationProfile : DConnectNotificationProfile
13+
@interface DPHostNotificationProfile : DConnectNotificationProfile<UNUserNotificationCenterDelegate>
1314

1415
@end

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost/Classes/profile/DPHostNotificationProfile.m

Lines changed: 61 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)