2
0
mirror of synced 2025-01-11 14:44:12 +00:00

[messaging][notifications] Make all methods return a promise where they didn’t previously

This commit is contained in:
Chris Bianca 2018-03-28 11:02:39 +01:00
parent 33cd0b0b13
commit d101813b5f
7 changed files with 95 additions and 58 deletions

View File

@ -96,13 +96,15 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule {
} }
@ReactMethod @ReactMethod
public void subscribeToTopic(String topic) { public void subscribeToTopic(String topic, Promise promise) {
FirebaseMessaging.getInstance().subscribeToTopic(topic); FirebaseMessaging.getInstance().subscribeToTopic(topic);
promise.resolve(null);
} }
@ReactMethod @ReactMethod
public void unsubscribeFromTopic(String topic) { public void unsubscribeFromTopic(String topic, Promise promise) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
promise.resolve(null);
} }
private class MessageReceiver extends BroadcastReceiver { private class MessageReceiver extends BroadcastReceiver {

View File

@ -64,7 +64,7 @@ public class RNFirebaseNotificationManager {
this.preferences = context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE); this.preferences = context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
} }
public void cancelAllNotifications() { public void cancelAllNotifications(Promise promise) {
try { try {
Map<String, ?> notifications = preferences.getAll(); Map<String, ?> notifications = preferences.getAll();
@ -72,16 +72,25 @@ public class RNFirebaseNotificationManager {
cancelAlarm(notificationId); cancelAlarm(notificationId);
} }
preferences.edit().clear().apply(); preferences.edit().clear().apply();
promise.resolve(null);
} catch (SecurityException e) { } catch (SecurityException e) {
// TODO: Identify what these situations are // TODO: Identify what these situations are
// In some devices/situations cancelAllLocalNotifications can throw a SecurityException. // In some devices/situations cancelAllLocalNotifications can throw a SecurityException.
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
promise.reject("notification/cancel_notifications_error", "Could not cancel notifications", e);
} }
} }
public void cancelNotification(String notificationId) { public void cancelNotification(String notificationId, Promise promise) {
cancelAlarm(notificationId); try {
preferences.edit().remove(notificationId).apply(); cancelAlarm(notificationId);
preferences.edit().remove(notificationId).apply();
} catch (SecurityException e) {
// TODO: Identify what these situations are
// In some devices/situations cancelAllLocalNotifications can throw a SecurityException.
Log.e(TAG, e.getMessage());
promise.reject("notification/cancel_notification_error", "Could not cancel notifications", e);
}
} }
public void createChannel(ReadableMap channelMap) { public void createChannel(ReadableMap channelMap) {
@ -162,12 +171,14 @@ public class RNFirebaseNotificationManager {
return array; return array;
} }
public void removeAllDeliveredNotifications() { public void removeAllDeliveredNotifications(Promise promise) {
notificationManager.cancelAll(); notificationManager.cancelAll();
promise.resolve(null);
} }
public void removeDeliveredNotification(String notificationId) { public void removeDeliveredNotification(String notificationId, Promise promise) {
notificationManager.cancel(notificationId.hashCode()); notificationManager.cancel(notificationId.hashCode());
promise.resolve(null);
} }

View File

@ -62,13 +62,13 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen
} }
@ReactMethod @ReactMethod
public void cancelAllNotifications() { public void cancelAllNotifications(Promise promise) {
notificationManager.cancelAllNotifications(); notificationManager.cancelAllNotifications(promise);
} }
@ReactMethod @ReactMethod
public void cancelNotification(String notificationId) { public void cancelNotification(String notificationId, Promise promise) {
notificationManager.cancelNotification(notificationId); notificationManager.cancelNotification(notificationId, promise);
} }
@ReactMethod @ReactMethod
@ -103,17 +103,17 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen
} }
@ReactMethod @ReactMethod
public void removeAllDeliveredNotifications() { public void removeAllDeliveredNotifications(Promise promise) {
notificationManager.removeAllDeliveredNotifications(); notificationManager.removeAllDeliveredNotifications(promise);
} }
@ReactMethod @ReactMethod
public void removeDeliveredNotification(String notificationId) { public void removeDeliveredNotification(String notificationId, Promise promise) {
notificationManager.removeDeliveredNotification(notificationId); notificationManager.removeDeliveredNotification(notificationId, promise);
} }
@ReactMethod @ReactMethod
public void setBadge(int badge) { public void setBadge(int badge, Promise promise) {
// Store the badge count for later retrieval // Store the badge count for later retrieval
sharedPreferences.edit().putInt(BADGE_KEY, badge).apply(); sharedPreferences.edit().putInt(BADGE_KEY, badge).apply();
if (badge == 0) { if (badge == 0) {
@ -123,6 +123,7 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen
Log.d(TAG, "Apply badge count: " + badge); Log.d(TAG, "Apply badge count: " + badge);
ShortcutBadger.applyCount(this.getReactApplicationContext(), badge); ShortcutBadger.applyCount(this.getReactApplicationContext(), badge);
} }
promise.resolve(null);
} }
@ReactMethod @ReactMethod

View File

@ -143,7 +143,7 @@ RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RC
} }
// Non Web SDK methods // Non Web SDK methods
RCT_EXPORT_METHOD(hasPermission: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(hasPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
resolve(@([RCTSharedApplication() currentUserNotificationSettings].types != UIUserNotificationTypeNone)); resolve(@([RCTSharedApplication() currentUserNotificationSettings].types != UIUserNotificationTypeNone));
@ -158,7 +158,7 @@ RCT_EXPORT_METHOD(hasPermission: (RCTPromiseResolveBlock)resolve rejecter:(RCTPr
} }
RCT_EXPORT_METHOD(sendMessage: (NSDictionary *) message RCT_EXPORT_METHOD(sendMessage:(NSDictionary *) message
resolve:(RCTPromiseResolveBlock) resolve resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject) { reject:(RCTPromiseRejectBlock) reject) {
if (!message[@"to"]) { if (!message[@"to"]) {
@ -175,12 +175,18 @@ RCT_EXPORT_METHOD(sendMessage: (NSDictionary *) message
resolve(nil); resolve(nil);
} }
RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) { RCT_EXPORT_METHOD(subscribeToTopic:(NSString*) topic
resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject) {
[[FIRMessaging messaging] subscribeToTopic:topic]; [[FIRMessaging messaging] subscribeToTopic:topic];
resolve(nil);
} }
RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) { RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic
resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject) {
[[FIRMessaging messaging] unsubscribeFromTopic:topic]; [[FIRMessaging messaging] unsubscribeFromTopic:topic];
resolve(nil);
} }
// ** Start internals ** // ** Start internals **

View File

@ -202,20 +202,24 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
// ** Finish UNUserNotificationCenterDelegate methods // ** Finish UNUserNotificationCenterDelegate methods
// ******************************************************* // *******************************************************
RCT_EXPORT_METHOD(cancelAllNotifications) { RCT_EXPORT_METHOD(cancelAllNotifications:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if ([self isIOS89]) { if ([self isIOS89]) {
[RCTSharedApplication() cancelAllLocalNotifications]; [RCTSharedApplication() cancelAllLocalNotifications];
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
if (notificationCenter != nil) { if (notificationCenter != nil) {
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests]; [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
} }
#endif }
} }
resolve(nil);
} }
RCT_EXPORT_METHOD(cancelNotification:(NSString*) notificationId) { RCT_EXPORT_METHOD(cancelNotification:(NSString*) notificationId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if ([self isIOS89]) { if ([self isIOS89]) {
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) { for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
NSDictionary *notificationInfo = notification.userInfo; NSDictionary *notificationInfo = notification.userInfo;
@ -224,13 +228,14 @@ RCT_EXPORT_METHOD(cancelNotification:(NSString*) notificationId) {
} }
} }
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
if (notificationCenter != nil) { if (notificationCenter != nil) {
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationId]]; [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationId]];
} }
#endif }
} }
resolve(nil);
} }
RCT_EXPORT_METHOD(displayNotification:(NSDictionary*) notification RCT_EXPORT_METHOD(displayNotification:(NSDictionary*) notification
@ -241,7 +246,7 @@ RCT_EXPORT_METHOD(displayNotification:(NSDictionary*) notification
[RCTSharedApplication() presentLocalNotificationNow:notif]; [RCTSharedApplication() presentLocalNotificationNow:notif];
resolve(nil); resolve(nil);
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNNotificationRequest* request = [self buildUNNotificationRequest:notification withSchedule:false]; UNNotificationRequest* request = [self buildUNNotificationRequest:notification withSchedule:false];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) { if (!error) {
@ -250,7 +255,7 @@ RCT_EXPORT_METHOD(displayNotification:(NSDictionary*) notification
reject(@"notifications/display_notification_error", @"Failed to display notificaton", error); reject(@"notifications/display_notification_error", @"Failed to display notificaton", error);
} }
}]; }];
#endif }
} }
} }
@ -295,7 +300,7 @@ RCT_EXPORT_METHOD(getScheduledNotifications:(RCTPromiseResolveBlock)resolve
} }
resolve(notifications); resolve(notifications);
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSMutableArray* notifications = [[NSMutableArray alloc] init]; NSMutableArray* notifications = [[NSMutableArray alloc] init];
for (UNNotificationRequest *notif in requests){ for (UNNotificationRequest *notif in requests){
@ -304,34 +309,39 @@ RCT_EXPORT_METHOD(getScheduledNotifications:(RCTPromiseResolveBlock)resolve
} }
resolve(notifications); resolve(notifications);
}]; }];
#endif }
} }
} }
RCT_EXPORT_METHOD(removeAllDeliveredNotifications) { RCT_EXPORT_METHOD(removeAllDeliveredNotifications:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if ([self isIOS89]) { if ([self isIOS89]) {
// No such functionality on iOS 8/9 // No such functionality on iOS 8/9
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
if (notificationCenter != nil) { if (notificationCenter != nil) {
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
} }
#endif }
} }
resolve(nil);
} }
RCT_EXPORT_METHOD(removeDeliveredNotification:(NSString*) notificationId) { RCT_EXPORT_METHOD(removeDeliveredNotification:(NSString*) notificationId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if ([self isIOS89]) { if ([self isIOS89]) {
// No such functionality on iOS 8/9 // No such functionality on iOS 8/9
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
if (notificationCenter != nil) { if (notificationCenter != nil) {
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]]; [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]];
} }
#endif }
} }
resolve(nil);
} }
RCT_EXPORT_METHOD(scheduleNotification:(NSDictionary*) notification RCT_EXPORT_METHOD(scheduleNotification:(NSDictionary*) notification
@ -342,7 +352,7 @@ RCT_EXPORT_METHOD(scheduleNotification:(NSDictionary*) notification
[RCTSharedApplication() scheduleLocalNotification:notif]; [RCTSharedApplication() scheduleLocalNotification:notif];
resolve(nil); resolve(nil);
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
UNNotificationRequest* request = [self buildUNNotificationRequest:notification withSchedule:true]; UNNotificationRequest* request = [self buildUNNotificationRequest:notification withSchedule:true];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) { if (!error) {
@ -351,13 +361,16 @@ RCT_EXPORT_METHOD(scheduleNotification:(NSDictionary*) notification
reject(@"notification/schedule_notification_error", @"Failed to schedule notificaton", error); reject(@"notification/schedule_notification_error", @"Failed to schedule notificaton", error);
} }
}]; }];
#endif }
} }
} }
RCT_EXPORT_METHOD(setBadge: (NSInteger) number) { RCT_EXPORT_METHOD(setBadge:(NSInteger) number
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[RCTSharedApplication() setApplicationIconBadgeNumber:number]; [RCTSharedApplication() setApplicationIconBadgeNumber:number];
resolve(nil);
}); });
} }

View File

@ -142,12 +142,12 @@ export default class Messaging extends ModuleBase {
} }
} }
subscribeToTopic(topic: string): void { subscribeToTopic(topic: string): Promise<void> {
getNativeModule(this).subscribeToTopic(topic); return getNativeModule(this).subscribeToTopic(topic);
} }
unsubscribeFromTopic(topic: string): void { unsubscribeFromTopic(topic: string): Promise<void> {
getNativeModule(this).unsubscribeFromTopic(topic); return getNativeModule(this).unsubscribeFromTopic(topic);
} }
/** /**

View File

@ -128,21 +128,23 @@ export default class Notifications extends ModuleBase {
/** /**
* Cancel all notifications * Cancel all notifications
*/ */
cancelAllNotifications(): void { cancelAllNotifications(): Promise<void> {
getNativeModule(this).cancelAllNotifications(); return getNativeModule(this).cancelAllNotifications();
} }
/** /**
* Cancel a notification by id. * Cancel a notification by id.
* @param notificationId * @param notificationId
*/ */
cancelNotification(notificationId: string): void { cancelNotification(notificationId: string): Promise<void> {
if (!notificationId) { if (!notificationId) {
throw new Error( return Promise.reject(
'Notifications: cancelNotification expects a `notificationId`' new Error(
'Notifications: cancelNotification expects a `notificationId`'
)
); );
} }
getNativeModule(this).cancelNotification(notificationId); return getNativeModule(this).cancelNotification(notificationId);
} }
/** /**
@ -264,21 +266,23 @@ export default class Notifications extends ModuleBase {
/** /**
* Remove all delivered notifications. * Remove all delivered notifications.
*/ */
removeAllDeliveredNotifications(): void { removeAllDeliveredNotifications(): Promise<void> {
getNativeModule(this).removeAllDeliveredNotifications(); return getNativeModule(this).removeAllDeliveredNotifications();
} }
/** /**
* Remove a delivered notification. * Remove a delivered notification.
* @param notificationId * @param notificationId
*/ */
removeDeliveredNotification(notificationId: string): void { removeDeliveredNotification(notificationId: string): Promise<void> {
if (!notificationId) { if (!notificationId) {
throw new Error( return Promise.reject(
'Notifications: removeDeliveredNotification expects a `notificationId`' new Error(
'Notifications: removeDeliveredNotification expects a `notificationId`'
)
); );
} }
getNativeModule(this).removeDeliveredNotification(notificationId); return getNativeModule(this).removeDeliveredNotification(notificationId);
} }
/** /**
@ -306,8 +310,8 @@ export default class Notifications extends ModuleBase {
} }
} }
setBadge(badge: number): void { setBadge(badge: number): Promise<void> {
getNativeModule(this).setBadge(badge); return getNativeModule(this).setBadge(badge);
} }
} }