[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
public void subscribeToTopic(String topic) {
public void subscribeToTopic(String topic, Promise promise) {
FirebaseMessaging.getInstance().subscribeToTopic(topic);
promise.resolve(null);
}
@ReactMethod
public void unsubscribeFromTopic(String topic) {
public void unsubscribeFromTopic(String topic, Promise promise) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
promise.resolve(null);
}
private class MessageReceiver extends BroadcastReceiver {

View File

@ -64,7 +64,7 @@ public class RNFirebaseNotificationManager {
this.preferences = context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
}
public void cancelAllNotifications() {
public void cancelAllNotifications(Promise promise) {
try {
Map<String, ?> notifications = preferences.getAll();
@ -72,16 +72,25 @@ public class RNFirebaseNotificationManager {
cancelAlarm(notificationId);
}
preferences.edit().clear().apply();
promise.resolve(null);
} 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_notifications_error", "Could not cancel notifications", e);
}
}
public void cancelNotification(String notificationId) {
cancelAlarm(notificationId);
preferences.edit().remove(notificationId).apply();
public void cancelNotification(String notificationId, Promise promise) {
try {
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) {
@ -162,12 +171,14 @@ public class RNFirebaseNotificationManager {
return array;
}
public void removeAllDeliveredNotifications() {
public void removeAllDeliveredNotifications(Promise promise) {
notificationManager.cancelAll();
promise.resolve(null);
}
public void removeDeliveredNotification(String notificationId) {
public void removeDeliveredNotification(String notificationId, Promise promise) {
notificationManager.cancel(notificationId.hashCode());
promise.resolve(null);
}

View File

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

View File

@ -143,7 +143,7 @@ RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RC
}
// 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) {
dispatch_async(dispatch_get_main_queue(), ^{
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
reject:(RCTPromiseRejectBlock) reject) {
if (!message[@"to"]) {
@ -175,12 +175,18 @@ RCT_EXPORT_METHOD(sendMessage: (NSDictionary *) message
resolve(nil);
}
RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) {
RCT_EXPORT_METHOD(subscribeToTopic:(NSString*) topic
resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject) {
[[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];
resolve(nil);
}
// ** Start internals **

View File

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

View File

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

View File

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