Search code examples
iosobjective-cuilocalnotification

Set UILocalNotification on a response from the server in IOS


I have multiple images which I sent to the server and get response after successful upload response 1.

Now I want to set a local notification on it, that if server response 1 after upload of an image it should show a notification like a banner that Image is sent.

I have tried some code but it is showing any alert when I get the response.

My code is:

    -(void)Images{

            NSString *eachImagePath;
            if(_arrai.count == 0)
                return;

            eachImagePath = [NSString stringWithFormat:@"%@",_arrai[0]];

            NSMutableDictionary *dictAddNewJobImages = [[NSMutableDictionary alloc]init];
            dictAddNewJobImages[@"property_id"] = Propid;
            dictAddNewJobImages[@"name"] = _arrainame;

            NSString *strWebService = [NSString stringWithFormat:@"My URL"];

            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];

            [manager.requestSerializer setTimeoutInterval:600.0];

             [manager POST:strWebService parameters:dictAddNewJobImages constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData)
             {
                 NSURL *filePath = [NSURL fileURLWithPath:eachImagePath];
                 [formData appendPartWithFileURL:filePath name:@"image" error:nil];
             } progress:^(NSProgress * _Nonnull uploadProgress)
             {
             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
             {
                 NSLog(@"%@",responseObject);
                 [_arrai removeObjectAtIndex:0];
                 if(_arrai.count > 0)
                     [self Images];


             } failure:^(NSURLSessionDataTask*  _Nullable task, NSError * _Nonnull error) {
                 NSLog(@"%@",error);

              }];

            NSString *res = [serverResponse valueForKey:@"response"];
            NSLog(@"response: %@", res);

            UILocalNotification* localNotification = [[UILocalNotification alloc] init];

           // localNotification.fireDate = res;

            localNotification.alertBody = res;

            localNotification.alertAction = @"Image Sent";

            localNotification.timeZone = [NSTimeZone defaultTimeZone];

            localNotification.applicationIconBadgeNumber = [[UIApplication 

        sharedApplication] applicationIconBadgeNumber] + 1;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

            // Request to reload table view data
            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

            // Dismiss the view controller
            [self dismissViewControllerAnimated:YES completion:nil];
}

Solution

  • Please add notification like this:

    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
            UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
    
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    
                localNotification.fireDate = [NSDate date];
    
                localNotification.alertBody = res;
    
                localNotification.alertTitle = @"Image Sent";
    
                localNotification.timeZone = [NSTimeZone defaultTimeZone];
    
                localNotification.applicationIconBadgeNumber = [[UIApplication 
    
            sharedApplication] applicationIconBadgeNumber] + 1;
    
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];