Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NotificationService.m
- // CriticalAlertPushNotitication
- //
- // Created by Iurii OLIIAR on 24/04/2019.
- //
- #import "NotificationService.h"
- #import "FCMPlugin.h"
- //
- // POST
- // https://devsocket.tapappsecurity.com/fcmReceived
- //
- // request{
- // alert_id : 999999,
- // token: 'sdvdsvdsvaivowjwwfpokfpoef'
- // }
- @interface NotificationService ()
- @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
- @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
- @end
- @implementation NotificationService
- - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
- self.contentHandler = contentHandler;
- self.bestAttemptContent = [request.content mutableCopy];
- NSDictionary *userInfo = _bestAttemptContent.userInfo;
- NSDictionary *apsInfo = userInfo[@"aps"];
- NSLog(@"userInfo %@", userInfo);
- NSString *critical = userInfo[@"critical"];
- NSNumber *alert_id = userInfo[@"alert_id"];
- NSLog(@"alert_id :%@", alert_id);
- if ([critical integerValue] == 1) {
- NSString *sound = apsInfo[@"sound"];
- if (@available(iOS 12.0, *)) {
- if (sound.length > 0) {
- _bestAttemptContent.sound = [UNNotificationSound criticalSoundNamed:sound
- withAudioVolume:1.0];
- } else {
- _bestAttemptContent.sound = [UNNotificationSound defaultCriticalSoundWithAudioVolume:1.0];
- }
- }
- }
- self.contentHandler(self.bestAttemptContent);
- }
- - (void)serviceExtensionTimeWillExpire {
- // Called just before the extension will be terminated by the system.
- // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
- self.contentHandler(self.bestAttemptContent);
- }
- #pragma mark - Helper
- - (void)sendAlertId:(NSNumber *)alertId
- token:(NSString *)token
- completion:(void (^)(NSInteger statusCode, NSError *error))completion {
- NSURL* url = [NSURL URLWithString:@"https://devsocket.tapappsecurity.com/fcmReceived"];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:30.0];
- NSDictionary *data = @{
- @"alert_id" : alertId,
- @"token": token
- };
- NSError *parseError = nil;
- NSData *requestData = [NSJSONSerialization dataWithJSONObject:data
- options:0
- error:&parseError];
- if (parseError != nil) {
- NSLog(@"Error parsing data %@", parseError.localizedDescription);
- }
- [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestData.length] forHTTPHeaderField:@"Content-Length"];
- request.HTTPBody = requestData;
- request.HTTPMethod = @"POST";
- NSURLSessionDataTask *postDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
- completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- dispatch_async(dispatch_get_main_queue(), ^{
- NSInteger status = ((NSHTTPURLResponse *)response).statusCode;
- completion(status, error);
- });
- }];
- [postDataTask resume];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement