1

I'm trying to send notifications in objective c but I can't figure out how.

So far I have

    - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if(self.username != nil)
    {
        NSLog(@"Background fetch username: %@", self.username);
        [self fetchNotifications];
    }


    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)fetchNotifications
{
    NSURL *url = [NSURL URLWithString:
                  [NSString stringWithFormat:@"%@%@",
                   @"https://myurl?username="
                   , self.username]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
         if (data.length > 0 && connectionError == nil)
         {
             NSDictionary *notifications = [NSJSONSerialization JSONObjectWithData:data
                                                                           options:0
                                                                             error:NULL];

             int nbMessages = [[notifications objectForKey:@"NbOfUnreadMessage"] intValue];
             int nbOfNewMatch = [[notifications objectForKey:@"NbOfNewMatch"] intValue];
             int nbOfNewPlanification = [[notifications objectForKey:@"NbOfNewPlanification"] intValue];
             //int nbMessages = [[notifications objectForKey:@"NbOfUnreadMessage"] intValue];

             NSLog(@"Notifications - nbMessage: %i",nbMessages);
             NSLog(@"Notifications - nbNewMatch: %i",nbOfNewMatch);
             NSLog(@"Notifications - nbNewPlan: %i",nbOfNewPlanification);

             // Schedule the notification
             UILocalNotification* localNotification = [[UILocalNotification alloc] init];
             localNotification.fireDate = nil;
             localNotification.alertBody = @"test";
             localNotification.alertAction = @"Show me the item";
             localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

             [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
         }
     }];
}

When I try on the iPhone 6 simulator with IOS 8.1 I have the following error

Attempting to badge the application icon but haven't received permission from the user to badge the application

I will have to read and understand this post

but my real problem is when I use an iPhone 5 IOS 7.1 simulator, the function fetchNotifications is not even called.

Why fetchNotifications is only called under IOS 8.1?

performFetchWithCompletionHandler is called under both OS

Community
  • 1
  • 1
Marc
  • 16,170
  • 20
  • 76
  • 119

1 Answers1

0

Attempting to badge the application icon but haven't received permission from the user to badge the application

is the way Xcode tells you that you can't change the notifications badge value while running on the simulator. It's not allowed

This delegate method is called when iOS decide. From the UIApplicationDelegate documentation:

When an opportunity arises to download data, the system calls this method to give your app a chance to download any data it needs. More importantly, the system uses the elapsed time to calculate power usage and data costs for your app’s background downloads. If your app takes a long time to call the completion handler, it may be given fewer future opportunities to fetch data in the future.

Christian
  • 382
  • 2
  • 11
  • I understand but I get the log message in performFetchWithCompletionHandler but it doesn't call fetchnotification under 7.1 but it does under 8.1 – Marc Feb 04 '15 at 20:32