3

I have an app that calls scheduleLocalNotification, but it doesn't work when I install it to /Applications instead of /var/mobile/Applications:

- (void) doNotify
{
    // this doesn't work when app is in /Applications but does in /var/mobile/Applications
    UILocalNotification * theNotification = [[UILocalNotification alloc] init];
    theNotification.alertBody = @"Finished processing.";
    theNotification.alertAction = @"Ok";
    theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    [[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
    NSLog(@"notification scheduled: %@", theNotification);
}

I tried presentLocalNotification instead in case it was a timing issue.

I implemented didReceiveLocalNotification in app delegate to see if that was being called instead, but it wasn't, it was only called when app is in foreground like it should.

If I put the app back in /var/mobile/Applications, it works as it should. I'm using Xcode 4.2.1 and running iOS 5.1.1 on an iPhone 4S and iPod Touch 4g

EDIT: App can run in the background because it is a music app

Nate
  • 31,017
  • 13
  • 83
  • 207
  • 1
    Just as an additional piece of information, I found some references that suggested also registering for **remote** notifications on iOS 5, when trying to get **local** notifications to work. I tried this, but I still see your problem ... that local notifications aren't working when the app's installed in /Applications. I also tried properly code-signing with a normal Apple provisioning profile (vs. fake signing with `ldid`). Still no success. So, I'm curious about this myself. – Nate Jul 05 '12 at 06:08
  • Note that I posted an [answer to another question](http://stackoverflow.com/a/15455596/119114), that basically implements a similar feature, without actually using the `UILocalNotification` class. – Nate Mar 17 '13 at 05:08

2 Answers2

0

I met the same problem just now. And I already solve this problem through the official document.

In fact, you should regist the notification to get access to use notification:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];
Shvier
  • 1
0

Register for Notifications

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

    [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                   settingsForTypes:UIUserNotificationTypeAlert|  UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
    }
    [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}

Schedule Notification

- (void)applicationDidEnterBackground:(UIApplication *)application
 {
  // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


    NSLog(@"startLocalNotification");
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
    notification.alertBody = @"notification Message ";

    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    //notification.applicationIconBadgeNumber = 10;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];


}
Sanju
  • 1,148
  • 11
  • 26