Advertisement
Guest User

Untitled

a guest
May 6th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NotificationService.m
  3. //  CriticalAlertPushNotitication
  4. //
  5. //  Created by Iurii OLIIAR on 24/04/2019.
  6. //
  7.  
  8. #import "NotificationService.h"
  9.  
  10.  
  11. //
  12. //        POST
  13. //        https://devsocket.tapappsecurity.com/fcmReceived
  14. //
  15. //        request{
  16. //            alert_id : 999999,
  17. //            token: 'sdvdsvdsvaivowjwwfpokfpoef'
  18. //        }
  19.  
  20. @interface NotificationService ()
  21.  
  22. @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
  23. @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
  24.  
  25. @end
  26.  
  27. @implementation NotificationService
  28.  
  29. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  30.     self.contentHandler = contentHandler;
  31.     self.bestAttemptContent = [request.content mutableCopy];
  32.    
  33.    
  34.     NSDictionary *userInfo = _bestAttemptContent.userInfo;
  35.     NSDictionary *apsInfo = userInfo[@"aps"];
  36.     NSLog(@"userInfo %@", userInfo);
  37.     NSString *critical = userInfo[@"critical"];
  38.     NSNumber *alert_id = userInfo[@"alert_id"];
  39.     NSString *action = userInfo[@"action"];
  40.    
  41.     NSUserDefaults *testUser = [[NSUserDefaults alloc] initWithSuiteName:@"group.tapapp.com"];
  42.     [testUser synchronize];
  43.     NSString *fcmToken = [testUser objectForKey:@"fcmToken"];
  44.    
  45.     NSLog(@"fcmToken :%@", fcmToken);
  46.    
  47.     if ([critical integerValue] == 1) {
  48.         NSString *sound = apsInfo[@"sound"];
  49.         if (@available(iOS 12.0, *)) {
  50.             if (sound.length > 0) {
  51.                 self.bestAttemptContent.sound = [UNNotificationSound criticalSoundNamed:sound
  52.                                                                         withAudioVolume:1.0];
  53.             } else {
  54.                 self.bestAttemptContent.sound = [UNNotificationSound defaultCriticalSoundWithAudioVolume:1.0];
  55.             }
  56.         }
  57.     }
  58.  
  59.     if([action isEqualToString:@"new_alert"]) {
  60.    
  61.         [self sendAlertId:alert_id token:fcmToken completion:^(NSInteger statusCode, NSError *error) {
  62.            
  63.             self.contentHandler(self.bestAttemptContent);
  64.         }];
  65.     }
  66. }
  67.  
  68. - (void)serviceExtensionTimeWillExpire {
  69.     // Called just before the extension will be terminated by the system.
  70.     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  71.     self.contentHandler(self.bestAttemptContent);
  72. }
  73.  
  74.  
  75. #pragma mark - Helper
  76.  
  77. - (void)sendAlertId:(NSNumber *)alertId
  78.               token:(NSString *)token
  79.          completion:(void (^)(NSInteger statusCode, NSError *error))completion {
  80.     NSURL* url = [NSURL URLWithString:@"https://devsocket.tapappsecurity.com/fcmReceived"];
  81.    
  82.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
  83.                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
  84.                                                        timeoutInterval:30.0];
  85.     NSString * dataString = [NSString stringWithFormat:@"alert_id=%@&token=%@",alertId,token];
  86.     NSData *data = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  87.    
  88.     request.HTTPBody = data;
  89.     request.HTTPMethod = @"POST";
  90.    
  91.     NSURLSessionDataTask *postDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
  92.                                                                          completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  93.                                                                              dispatch_async(dispatch_get_main_queue(), ^{
  94.                                                                                  NSInteger status = ((NSHTTPURLResponse *)response).statusCode;
  95.                                                                                  completion(status, error);
  96.                                                                              });
  97.                                                                          }];
  98.    
  99.     [postDataTask resume];
  100. }
  101.  
  102. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement