Browse Source

Add notificationCenter setting check

Mathieu Acthernoene 5 years ago
parent
commit
49a0a02911
4 changed files with 37 additions and 60 deletions
  1. 4
    2
      README.md
  2. 19
    55
      example/App.tsx
  3. 12
    2
      ios/Notifications/RNPermissionHandlerNotifications.m
  4. 2
    1
      src/index.ts

+ 4
- 2
README.md View File

268
   sound?: boolean;
268
   sound?: boolean;
269
   lockScreen?: boolean;
269
   lockScreen?: boolean;
270
   carPlay?: boolean;
270
   carPlay?: boolean;
271
-  critical?: boolean;
271
+  notificationCenter?: boolean;
272
+  criticalAlert?: boolean;
272
 }
273
 }
273
 
274
 
274
 function checkNotifications(): Promise<{
275
 function checkNotifications(): Promise<{
309
   sound?: boolean;
310
   sound?: boolean;
310
   lockScreen?: boolean;
311
   lockScreen?: boolean;
311
   carPlay?: boolean;
312
   carPlay?: boolean;
312
-  critical?: boolean;
313
+  notificationCenter?: boolean;
314
+  criticalAlert?: boolean;
313
 }
315
 }
314
 
316
 
315
 function requestNotifications(
317
 function requestNotifications(

+ 19
- 55
example/App.tsx View File

68
   notifications: NotificationsResponse;
68
   notifications: NotificationsResponse;
69
 }
69
 }
70
 
70
 
71
+function getSettingString(setting: boolean | undefined) {
72
+  return setting
73
+    ? RESULTS.GRANTED
74
+    : setting === false
75
+    ? RESULTS.DENIED
76
+    : RESULTS.UNAVAILABLE;
77
+}
78
+
71
 export default class App extends React.Component<{}, State> {
79
 export default class App extends React.Component<{}, State> {
72
   state: State = {
80
   state: State = {
73
     statuses: [],
81
     statuses: [],
91
 
99
 
92
   render() {
100
   render() {
93
     const {notifications} = this.state;
101
     const {notifications} = this.state;
102
+    const {settings} = notifications;
94
 
103
 
95
-    const {
96
-      alert,
97
-      badge,
98
-      sound,
99
-      lockScreen,
100
-      carPlay,
101
-      critical,
102
-    } = notifications.settings;
104
+    console.warn(settings);
103
 
105
 
104
     return (
106
     return (
105
       <View style={{flex: 1, backgroundColor: theme.colors.background}}>
107
       <View style={{flex: 1, backgroundColor: theme.colors.background}}>
167
         </TouchableRipple>
169
         </TouchableRipple>
168
 
170
 
169
         <Text style={{margin: 15, marginTop: 0, color: '#777'}}>
171
         <Text style={{margin: 15, marginTop: 0, color: '#777'}}>
170
-          {`alert: ${
171
-            alert
172
-              ? RESULTS.GRANTED
173
-              : alert === false
174
-              ? RESULTS.DENIED
175
-              : RESULTS.UNAVAILABLE
176
-          }\n`}
177
-
178
-          {`badge: ${
179
-            badge
180
-              ? RESULTS.GRANTED
181
-              : badge === false
182
-              ? RESULTS.DENIED
183
-              : RESULTS.UNAVAILABLE
184
-          }\n`}
185
-
186
-          {`sound: ${
187
-            sound
188
-              ? RESULTS.GRANTED
189
-              : sound === false
190
-              ? RESULTS.DENIED
191
-              : RESULTS.UNAVAILABLE
192
-          }\n`}
193
-
194
-          {`lockScreen: ${
195
-            lockScreen
196
-              ? RESULTS.GRANTED
197
-              : lockScreen === false
198
-              ? RESULTS.DENIED
199
-              : RESULTS.UNAVAILABLE
200
-          }\n`}
201
-
202
-          {`carPlay: ${
203
-            carPlay
204
-              ? RESULTS.GRANTED
205
-              : carPlay === false
206
-              ? RESULTS.DENIED
207
-              : RESULTS.UNAVAILABLE
208
-          }\n`}
209
-
210
-          {`critical: ${
211
-            critical
212
-              ? RESULTS.GRANTED
213
-              : critical === false
214
-              ? RESULTS.DENIED
215
-              : RESULTS.UNAVAILABLE
216
-          }\n`}
172
+          {`alert: ${getSettingString(settings.alert)}\n`}
173
+          {`badge: ${getSettingString(settings.badge)}\n`}
174
+          {`sound: ${getSettingString(settings.sound)}\n`}
175
+          {`lockScreen: ${getSettingString(settings.lockScreen)}\n`}
176
+          {`notificationCenter: ${getSettingString(
177
+            settings.notificationCenter,
178
+          )}\n`}
179
+          {`carPlay: ${getSettingString(settings.carPlay)}\n`}
180
+          {`criticalAlert: ${getSettingString(settings.criticalAlert)}\n`}
217
         </Text>
181
         </Text>
218
       </View>
182
       </View>
219
     );
183
     );

+ 12
- 2
ios/Notifications/RNPermissionHandlerNotifications.m View File

27
       bool sound = settings.soundSetting == UNNotificationSettingEnabled;
27
       bool sound = settings.soundSetting == UNNotificationSettingEnabled;
28
       bool lockScreen = settings.lockScreenSetting == UNNotificationSettingEnabled;
28
       bool lockScreen = settings.lockScreenSetting == UNNotificationSettingEnabled;
29
       bool carPlay = settings.carPlaySetting == UNNotificationSettingEnabled;
29
       bool carPlay = settings.carPlaySetting == UNNotificationSettingEnabled;
30
+      bool notificationCenter = settings.notificationCenterSetting == UNNotificationSettingEnabled;
30
 
31
 
31
       if (settings.alertSetting != UNNotificationSettingNotSupported)
32
       if (settings.alertSetting != UNNotificationSettingNotSupported)
32
         [result setValue:@(alert) forKey:@"alert"];
33
         [result setValue:@(alert) forKey:@"alert"];
38
         [result setValue:@(lockScreen) forKey:@"lockScreen"];
39
         [result setValue:@(lockScreen) forKey:@"lockScreen"];
39
       if (settings.carPlaySetting != UNNotificationSettingNotSupported)
40
       if (settings.carPlaySetting != UNNotificationSettingNotSupported)
40
         [result setValue:@(carPlay) forKey:@"carPlay"];
41
         [result setValue:@(carPlay) forKey:@"carPlay"];
42
+      if (settings.notificationCenterSetting != UNNotificationSettingNotSupported)
43
+        [result setValue:@(notificationCenter) forKey:@"notificationCenter"];
41
 
44
 
42
       if (@available(iOS 12.0, *)) {
45
       if (@available(iOS 12.0, *)) {
43
         bool criticalAlert = settings.criticalAlertSetting == UNNotificationSettingEnabled;
46
         bool criticalAlert = settings.criticalAlertSetting == UNNotificationSettingEnabled;
104
       if (provisional) types += UNAuthorizationOptionProvisional;
107
       if (provisional) types += UNAuthorizationOptionProvisional;
105
     }
108
     }
106
 
109
 
107
-    if (!alert && !badge && !sound && !criticalAlert && !carPlay && !provisional) {
110
+    if (!alert &&
111
+        !badge &&
112
+        !sound &&
113
+        !criticalAlert &&
114
+        !carPlay &&
115
+        !provisional) {
108
       types += UNAuthorizationOptionAlert;
116
       types += UNAuthorizationOptionAlert;
109
       types += UNAuthorizationOptionBadge;
117
       types += UNAuthorizationOptionBadge;
110
       types += UNAuthorizationOptionSound;
118
       types += UNAuthorizationOptionSound;
135
       if (badge) types += UIUserNotificationTypeBadge;
143
       if (badge) types += UIUserNotificationTypeBadge;
136
       if (sound) types += UIUserNotificationTypeSound;
144
       if (sound) types += UIUserNotificationTypeSound;
137
 
145
 
138
-      if (!alert && !badge && !sound) {
146
+      if (!alert &&
147
+          !badge &&
148
+          !sound) {
139
         types += UIUserNotificationTypeAlert;
149
         types += UIUserNotificationTypeAlert;
140
         types += UIUserNotificationTypeBadge;
150
         types += UIUserNotificationTypeBadge;
141
         types += UIUserNotificationTypeSound;
151
         types += UIUserNotificationTypeSound;

+ 2
- 1
src/index.ts View File

88
   sound?: boolean;
88
   sound?: boolean;
89
   lockScreen?: boolean;
89
   lockScreen?: boolean;
90
   carPlay?: boolean;
90
   carPlay?: boolean;
91
-  critical?: boolean;
91
+  notificationCenter?: boolean;
92
+  criticalAlert?: boolean;
92
 }
93
 }
93
 
94
 
94
 export interface NotificationsResponse {
95
 export interface NotificationsResponse {