Skip to content

Commit a3a8c80

Browse files
LocalNotificationでカメラのPreviewを停止できるようにした。
1 parent 2216226 commit a3a8c80

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost/Classes/profile/DPHostMediaStreamRecording/Recorders/Photo/DPHostCameraRecorder.m

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
// Released under the MIT license
77
// http://opensource.org/licenses/mit-license.php
88
//
9-
9+
#import <UserNotifications/UserNotifications.h>
1010
#import <Photos/Photos.h>
1111
#import <MobileCoreServices/UTCoreTypes.h>
1212
#import "DPHostCameraRecorder.h"
1313
#import "DPHostRecorder.h"
1414
#import "DPHostUtils.h"
1515
#import "DPHostRecorderUtils.h"
16-
@interface DPHostCameraRecorder()
16+
17+
static NSString *const kDPHostStartPreviewNotificationId = @"kDPHostStartPreviewNotificationId";
18+
@interface DPHostCameraRecorder()<UNUserNotificationCenterDelegate>
1719
@property (nonatomic) DPHostSimpleHttpServer *httpServer;
1820
/// Preview APIでプレビュー画像URIの配送を行うかどうか。
1921
@property (nonatomic) BOOL sendPreview;
@@ -117,14 +119,15 @@ - (void)takePhotoWithSuccessCompletion:(void (^)(NSURL *assetURL))successComplet
117119
}
118120
[weakSelf saveFileWithCompletionHandler:^(NSURL *assetURL, NSError *error) {
119121

120-
if ([weakSelf.session isRunning]) {
122+
if ([weakSelf.session isRunning] && !weakSelf.sendPreview) {
121123
[weakSelf.session stopRunning];
122124
}
123125
if (error) {
124126
failCompletion(error.localizedDescription);
125127
} else {
126128
successCompletion(assetURL);
127129
}
130+
128131
}];
129132
}];
130133
}
@@ -179,6 +182,7 @@ - (void)startWebServerWithSuccessCompletion:(void (^)(NSString *uri))successComp
179182
failCompletion(@"MJPEG Server cannot running.");
180183
return;
181184
}
185+
[self showPreviewNotification];
182186
successCompletion(url);
183187
}
184188

@@ -193,7 +197,7 @@ - (void)stopWebServer
193197
self.sendPreview = NO;
194198
// 次回プレビュー開始時に影響を与えない為に、初期値(無効値)を設定する。
195199
self.lastPreviewTimestamp = kCMTimeInvalid;
196-
200+
[self hidePreviewNotification];
197201
}
198202

199203
#pragma mark - Private Method
@@ -420,4 +424,60 @@ - (void) sendPreviewDataWithSampleBuffer:(CMSampleBufferRef)sampleBuffer
420424
}
421425

422426
}
427+
428+
#pragma mark - Notification Control
429+
- (void)showPreviewNotification
430+
{
431+
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
432+
content.title = @"カメラ撮影中(iOS Host Camera Preview)";
433+
content.body = @"タップして撮影を停止します。";
434+
// Deliver the notification in five seconds.
435+
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
436+
triggerWithTimeInterval:1
437+
repeats:NO];
438+
UNNotificationRequest* nRequest = [UNNotificationRequest
439+
requestWithIdentifier:kDPHostStartPreviewNotificationId
440+
content:content
441+
trigger:trigger];
442+
443+
// Schedule the notification.
444+
dispatch_async(dispatch_get_main_queue(), ^{
445+
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
446+
center.delegate = self;
447+
[center addNotificationRequest:nRequest
448+
withCompletionHandler:^(NSError * _Nullable error) {
449+
}];
450+
451+
});
452+
}
453+
454+
- (void)hidePreviewNotification
455+
{
456+
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
457+
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
458+
for (UNNotification *notification in notifications) {
459+
NSString *currentId = notification.request.identifier;
460+
if ([currentId isEqualToString:kDPHostStartPreviewNotificationId]) {
461+
[center removeDeliveredNotificationsWithIdentifiers:@[kDPHostStartPreviewNotificationId]];
462+
return;
463+
}
464+
}
465+
}];
466+
467+
}
468+
#pragma mark - Notification Delegate
469+
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
470+
didReceiveNotificationResponse:(UNNotificationResponse *)response
471+
withCompletionHandler:(void (^)(void))completionHandler {
472+
completionHandler();
473+
[self stopWebServer];
474+
}
475+
476+
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
477+
willPresentNotification:(UNNotification *)notification
478+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
479+
completionHandler(UNNotificationPresentationOptionBadge |
480+
UNNotificationPresentationOptionSound |
481+
UNNotificationPresentationOptionAlert);
482+
};
423483
@end

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ - (instancetype)init
5252
_notificationInfoDict = @{}.mutableCopy;
5353

5454
[self setNotificationIdLength: 3];
55-
56-
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
57-
center.delegate = self;
55+
dispatch_async(dispatch_get_main_queue(), ^{
56+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
57+
center.delegate = self;
58+
});
5859

5960
// API登録(didReceivePostNotifyRequest相当)
6061
NSString *postNotifyRequestApiPath = [self apiPath: nil

dConnectDevicePlugin/dConnectDeviceTheta/dConnectDeviceTheta/Classes/DPOmnidirectionalImageProfile.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// DPOmnidirectionalImageProfile.m
33
// dConnectDeviceTheta
44
//
5-
// Created by 星 貴之 on 2015/08/23.
6-
// Copyright (c) 2015年 DOCOMO. All rights reserved.
5+
// Copyright (c) 2015 NTT DOCOMO, INC.
6+
// Released under the MIT license
7+
// http://opensource.org/licenses/mit-license.php
78
//
89

10+
911
#import "DPOmnidirectionalImageProfile.h"
1012

1113
NSString *const DPOmnidirectionalImageProfileName = @"omnidirectionalImage";

0 commit comments

Comments
 (0)