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"
- //
- // 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"];
- NSString *action = userInfo[@"action"];
- NSUserDefaults *testUser = [[NSUserDefaults alloc] initWithSuiteName:@"group.tapapp.com"];
- [testUser synchronize];
- NSString *fcmToken = [testUser objectForKey:@"fcmToken"];
- NSLog(@"fcmToken :%@", fcmToken);
- if ([critical integerValue] == 1) {
- NSString *sound = apsInfo[@"sound"];
- if (@available(iOS 12.0, *)) {
- if (sound.length > 0) {
- self.bestAttemptContent.sound = [UNNotificationSound criticalSoundNamed:sound
- withAudioVolume:1.0];
- } else {
- self.bestAttemptContent.sound = [UNNotificationSound defaultCriticalSoundWithAudioVolume:1.0];
- }
- }
- }
- if([action isEqualToString:@"new_alert"]) {
- [self sendAlertId:alert_id token:fcmToken completion:^(NSInteger statusCode, NSError *error) {
- 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];
- NSString * dataString = [NSString stringWithFormat:@"alert_id=%@&token=%@",alertId,token];
- NSData *data = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- request.HTTPBody = data;
- 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