-
Notifications
You must be signed in to change notification settings - Fork 16
/
NotificationProvider.m
99 lines (78 loc) · 3.47 KB
/
NotificationProvider.m
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
//
// NotificationProvider.h
// nimbus
//
// Created by Johan Nordberg on 2012-10-27.
// Copyright 2012 FFFF00 Agents AB. All rights reserved.
//
#import "NotificationProvider.h"
@interface NotificationProvider () {
NSMutableDictionary *_userNotifications;
NSMutableDictionary *_webNotifications;
NSUserNotificationCenter *_notificationCenter;
}
@end
NSString *notificationKey(WebNotification *notification) {
return [NSString stringWithFormat:@"%lld", notification.notificationID];
}
@implementation NotificationProvider
- (id)init {
self = [super init];
if (self) {
_userNotifications = [[NSMutableDictionary alloc] init];
_webNotifications = [[NSMutableDictionary alloc] init];
_notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
_notificationCenter.delegate = self;
}
return self;
}
// probably safe to ignore since we only have one webview
- (void)registerWebView:(WebView *)webView {}
- (void)unregisterWebView:(WebView *)webView {}
- (void)showNotification:(WebNotification *)webNotification fromWebView:(WebView *)webView {
NSString *key = notificationKey(webNotification);
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = webNotification.title;
userNotification.informativeText = webNotification.body;
userNotification.userInfo = [NSDictionary dictionaryWithObject:key forKey:@"webNotification"];
userNotification.soundName = @"digit";
[_webNotifications setValue:webNotification forKey:key];
[_userNotifications setValue:userNotification forKey:key];
[_notificationCenter deliverNotification:userNotification];
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
NSString *key = [notification.userInfo objectForKey:@"webNotification"];
WebNotification *webNotification = [_webNotifications valueForKey:key];
[webNotification dispatchClickEvent];
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification {
NSString *key = [notification.userInfo objectForKey:@"webNotification"];
WebNotification *webNotification = [_webNotifications valueForKey:key];
[webNotification dispatchShowEvent];
}
- (WebNotificationPermission)policyForOrigin:(WebSecurityOrigin *)origin {
return WebNotificationPermissionAllowed;
}
- (void)cancelNotification:(WebNotification *)webNotification {
NSString *key = notificationKey(webNotification);
NSUserNotification *userNotification = [_userNotifications valueForKey:key];
[webNotification dispatchCloseEvent];
if (userNotification) [_notificationCenter removeDeliveredNotification:userNotification];
[_webNotifications removeObjectForKey:key];
[_userNotifications removeObjectForKey:key];
}
- (void)notificationDestroyed:(WebNotification *)webNotification {
// never called?
NSString *key = notificationKey(webNotification);
[_webNotifications removeObjectForKey:key];
[_userNotifications removeObjectForKey:key];
}
- (void)clearNotifications:(NSArray *)notificationIDs {
// never called?
[_notificationCenter removeAllDeliveredNotifications];
}
// why does the provider have reciever methods? was never called in my tests
- (void)webView:(WebView *)webView didShowNotification:(uint64_t)notificationID {}
- (void)webView:(WebView *)webView didClickNotification:(uint64_t)notificationID {}
- (void)webView:(WebView *)webView didCloseNotifications:(NSArray *)notificationIDs {}
@end