RNPNotification.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // RNPNotification.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 11/07/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. #import "RNPNotification.h"
  9. static NSString* RNPDidAskForNotification = @"RNPDidAskForNotification";
  10. @interface RNPNotification()
  11. @property (copy) void (^completionHandler)(NSString*);
  12. @end
  13. @implementation RNPNotification
  14. + (NSString *)getStatus
  15. {
  16. BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
  17. if (didAskForPermission) {
  18. if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
  19. // iOS8+
  20. BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
  21. BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
  22. if (isRegistered || isEnabled) {
  23. return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
  24. }
  25. else {
  26. return RNPStatusDenied;
  27. }
  28. } else {
  29. if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
  30. return RNPStatusDenied;
  31. }
  32. else {
  33. return RNPStatusAuthorized;
  34. }
  35. }
  36. } else {
  37. return RNPStatusUndetermined;
  38. }
  39. }
  40. - (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
  41. {
  42. BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
  43. if (!didAskForPermission) {
  44. self.completionHandler = completionHandler;
  45. [[NSNotificationCenter defaultCenter] addObserver:self
  46. selector:@selector(applicationDidBecomeActive)
  47. name:UIApplicationDidBecomeActiveNotification
  48. object:nil];
  49. if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
  50. // iOS8+
  51. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  52. [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  53. [[UIApplication sharedApplication] registerForRemoteNotifications];
  54. } else {
  55. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)types];
  56. }
  57. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
  58. [[NSUserDefaults standardUserDefaults] synchronize];
  59. } else {
  60. completionHandler([self.class getStatus]);
  61. }
  62. }
  63. - (void)applicationDidBecomeActive
  64. {
  65. [[NSNotificationCenter defaultCenter] removeObserver:self
  66. name:UIApplicationDidBecomeActiveNotification
  67. object:nil];
  68. if (self.completionHandler) {
  69. //for some reason, checking permission right away returns denied. need to wait a tiny bit
  70. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  71. self.completionHandler([self.class getStatus]);
  72. self.completionHandler = nil;
  73. });
  74. }
  75. }
  76. @end