Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
73
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. #import "FCMPlugin.h"
  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.    
  40.    
  41.     NSLog(@"alert_id :%@", alert_id);
  42.    
  43.     if ([critical integerValue] == 1) {
  44.         NSString *sound = apsInfo[@"sound"];
  45.         if (@available(iOS 12.0, *)) {
  46.             if (sound.length > 0) {
  47.                 _bestAttemptContent.sound = [UNNotificationSound criticalSoundNamed:sound
  48.                                                                     withAudioVolume:1.0];
  49.             } else {
  50.                 _bestAttemptContent.sound = [UNNotificationSound defaultCriticalSoundWithAudioVolume:1.0];
  51.             }
  52.         }
  53.     }
  54.    
  55.     self.contentHandler(self.bestAttemptContent);
  56.    
  57.    
  58. }
  59.  
  60. - (void)serviceExtensionTimeWillExpire {
  61.     // Called just before the extension will be terminated by the system.
  62.     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  63.     self.contentHandler(self.bestAttemptContent);
  64. }
  65.  
  66.  
  67. #pragma mark - Helper
  68.  
  69. - (void)sendAlertId:(NSNumber *)alertId
  70.               token:(NSString *)token
  71.          completion:(void (^)(NSInteger statusCode, NSError *error))completion {
  72.     NSURL* url = [NSURL URLWithString:@"https://devsocket.tapappsecurity.com/fcmReceived"];
  73.    
  74.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
  75.                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
  76.                                                        timeoutInterval:30.0];
  77.    
  78.     NSDictionary *data = @{
  79.                            @"alert_id" : alertId,
  80.                            @"token": token
  81.                            };
  82.    
  83.     NSError *parseError = nil;
  84.     NSData *requestData = [NSJSONSerialization dataWithJSONObject:data
  85.                                                           options:0
  86.                                                             error:&parseError];
  87.     if (parseError != nil) {
  88.         NSLog(@"Error parsing data %@", parseError.localizedDescription);
  89.     }
  90.    
  91.     [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestData.length] forHTTPHeaderField:@"Content-Length"];
  92.    
  93.     request.HTTPBody = requestData;
  94.     request.HTTPMethod = @"POST";
  95.    
  96.     NSURLSessionDataTask *postDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
  97.                                                                          completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  98.                                                                              dispatch_async(dispatch_get_main_queue(), ^{
  99.                                                                                  NSInteger status = ((NSHTTPURLResponse *)response).statusCode;
  100.                                                                                  completion(status, error);
  101.                                                                              });
  102.                                                                          }];
  103.    
  104.     [postDataTask resume];
  105. }
  106.  
  107. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement