Skip to content

Commit f4bcfea

Browse files
committed
WIP
1 parent 9f9525d commit f4bcfea

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

ios/Firestack/FirestackCloudMessaging.m

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,39 @@ + (void) setup:(UIApplication *) application
6868
selector:@selector(handleTokenRefresh)
6969
name:kFIRInstanceIDTokenRefreshNotification
7070
object: nil];
71+
}
7172

73+
#pragma mark Request permissions
74+
- (void) requestPermissions(NSDictionary *)requestedPermissions
75+
callback:(RCTResponseSenderBlock) callback
76+
{
7277
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"9.0")) {
73-
UIUserNotificationType allNotificationTypes =
74-
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
75-
UIUserNotificationSettings *settings =
76-
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
77-
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
78-
} else {
79-
// iOS 10 or later
80-
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
81-
UNAuthorizationOptions authOptions =
82-
UNAuthorizationOptionAlert
83-
| UNAuthorizationOptionSound
84-
| UNAuthorizationOptionBadge;
85-
[[UNUserNotificationCenter currentNotificationCenter]
86-
requestAuthorizationWithOptions:authOptions
87-
completionHandler:^(BOOL granted, NSError * _Nullable error) {
88-
}
89-
];
90-
91-
// For iOS 10 display notification (sent via APNS)
92-
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
93-
// For iOS 10 data message (sent via FCM)
94-
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
95-
#endif
96-
}
78+
UIUserNotificationType allNotificationTypes =
79+
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
80+
UIUserNotificationSettings *settings =
81+
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
82+
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
83+
} else {
84+
// iOS 10 or later
85+
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
86+
UNAuthorizationOptions authOptions =
87+
UNAuthorizationOptionAlert
88+
| UNAuthorizationOptionSound
89+
| UNAuthorizationOptionBadge;
90+
[[UNUserNotificationCenter currentNotificationCenter]
91+
requestAuthorizationWithOptions:authOptions
92+
completionHandler:^(BOOL granted, NSError * _Nullable error) {
93+
}
94+
];
95+
96+
// For iOS 10 display notification (sent via APNS)
97+
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
98+
// For iOS 10 data message (sent via FCM)
99+
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
100+
#endif
101+
}
97102

98-
[[UIApplication sharedApplication] registerForRemoteNotifications];
103+
[[UIApplication sharedApplication] registerForRemoteNotifications];
99104
}
100105

101106
#pragma mark callbacks

ios/Firestack/FirestackStorage.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ - (void) addUploadObservers:(FIRStorageUploadTask *) uploadTask
157157
case FIRStorageErrorCodeUnknown:
158158
// Unknown error occurred, inspect the server response
159159
[errProps setValue:@"Unknown error" forKey:@"description"];
160+
NSLog(@"Unknown error: %@", snapshot.error);
160161
break;
161162
}
162163

lib/modules/cloudmessaging.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import {NativeModules, NativeEventEmitter} from 'react-native';
1+
import {Platform, NativeModules, NativeEventEmitter} from 'react-native';
22
const FirestackCloudMessaging = NativeModules.FirestackCloudMessaging;
33
const FirestackCloudMessagingEvt = new NativeEventEmitter(FirestackCloudMessaging);
44

55
import promisify from '../utils/promisify'
66
import { Base, ReferenceBase } from './base'
7+
8+
const defaultPermissions = {
9+
'badge': 1,
10+
'sound': 2,
11+
'alert': 3
12+
}
713
export class CloudMessaging extends Base {
814
constructor(firestack, options = {}) {
915
super(firestack, options);
16+
17+
this.requestedPermissions = Object.assign({}, defaultPermissions, options.permissions);
1018
}
1119
get namespace() {
1220
return 'firestack:cloudMessaging'
@@ -16,6 +24,20 @@ export class CloudMessaging extends Base {
1624
return promisify('getToken', FirestackCloudMessaging)();
1725
}
1826

27+
// Request FCM permissions
28+
requestPermissions(requestedPermissions = {}) {
29+
if (Platform.OS === 'ios') {
30+
const mergedRequestedPermissions = Object.assign({},
31+
this.requestedPermissions,
32+
requestedPermissions);
33+
return promisify('requestPermissions', FirestackCloudMessaging)(mergedRequestedPermissions)
34+
.then(perms => {
35+
36+
return perms;
37+
});
38+
}
39+
}
40+
1941
sendMessage(details:Object = {}, type:string='local') {
2042
const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`
2143
this.log.info('sendMessage', methodName, details);

0 commit comments

Comments
 (0)