1

This notification is perfectly fires on Simulator (9.2) and doesn't fire on iPhone 6(9.2):

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

and I'm calling this code from:

- (void)applicationDidEnterBackground:(UIApplication *)application {
[self performSelector:@selector(sendLocalNotification) withObject:nil afterDelay:5];
}

Remote notifications work fine for all customers

protspace
  • 2,047
  • 1
  • 25
  • 30

4 Answers4

0

If you want to fire local notification e.g.: after 5 second use this code:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    UILocalNotification* localNotification = [UILocalNotification new];
    localNotification.fireDate = [[NSDate new] dateByAddingTimeInterval:5]; // Notification will be fired after 5 since now.
    localNotification.alertBody = @"Your alert message";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

NOTICE: If you want to receive notifications you have to ask user for permission e.g.:

UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

permission code listed above can be invoked in your AppDelegate.m e.g.: in didFinishLaunchingWithOptions method. You have to know that performSelector method is not suitable in this case, every UILocalNotification has fireDate property which should be used to fire notification with given NSDate delay.


UPDATE

Today I had the same problem, I passed as alertBody empty string and notification didn't appears. So in my opinion your

NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);

returns empty string or nil.

Please try assign to alertBody property any other string e.g.

localNotification.alertBody = @"test" 

it should works :)

Part of Apple doc:

Declaration SWIFT

var alertBody: String?

Discussion Assign a string or, preferably, a localized-string key (using NSLocalizedString) as the value of the message. If the value of this property is non-nil, an alert is displayed. The default value is nil (no alert). Printf style escape characters are stripped from the string prior to display; to include a percent symbol (%) in the message, use two percent symbols (%%).

Community
  • 1
  • 1
Robert
  • 3,790
  • 1
  • 27
  • 46
  • Your code din't work too. And permission asked exactly as you described. Also I want to say that remove pushes are working fine for all customers. Only local notifications don't come to device (but the come to simulator). – protspace Dec 16 '15 at 08:34
  • You are sure that you have turned on notifications for your app in settings? – Robert Dec 16 '15 at 08:37
  • notifications are ON. Also i noticed that notifications actually appear in upper curtain in Notifications segment. But they are not shown on screen while on sprinboard – protspace Dec 16 '15 at 08:47
  • thank you. But I discovered the problem: the problem was that i used performSelector but should use performSelectorInBackground performSelector doesn't work if app in background – protspace Dec 17 '15 at 13:18
  • but fot this purpose you should use fireDate, but OK :) – Robert Dec 17 '15 at 13:21
0

From your current code it looks like the selector may not be called unless your app is configured to run in the background. Roher's answer gives you the proper protocol for this, but if that doesn't work, try manually removing the app from your testing device, then re-installing it. I've had problems with some users accidentally tapping no in the alertview that appears asking for permission to send notifications when first opening the app. You may have done this unintentionally without noticing it. Deleting and re-installing will re-initiate the first-time permission request. If you don't want to delete the app I think you can do it from Settings.

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29
  • manual reinstalling didn't help. Tried two devices with ios9.2 - local notification banner doesn't appear (though i can see notification in upper curtain "Today") – protspace Dec 16 '15 at 08:59
0

Problem is that you trying to call something after app entered background. This is incorrect and will not work, there are a lot of articles about running background tasks on iOS.

You can schedule notification like this:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected.", nil);
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
    [application scheduleLocalNotification:localNotification];
}

If user opens your app before this 5 second interval expire, you can cancel this notification:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application cancelLocalNotification:application];
    // If you don't want to keep reference to the notification
    // [application cancelAllLocalNotifications];
}
kelin
  • 11,323
  • 6
  • 67
  • 104
  • I'm doing exactly as you described. Scheduling in applicationDidEnterBackground and Canceling in applicationDidBecomeActive. In simulator works fine, in device not (notifs appear in upper curtain but not showing as a banner on springboard) – protspace Dec 16 '15 at 08:55
  • @protspace, try to restart your device. – kelin Dec 16 '15 at 09:28
  • restarted several times - didn't help. Also i noticed that I see the banners just after i open app (send it to foreground again). Looks like they just waiting me for open the app. But they don't showing while the app is in background ( – protspace Dec 16 '15 at 10:06
  • Well, then you can try report it to Apple. – kelin Dec 16 '15 at 10:12
-1

the problem was that i used performSelector but should use performSelectorInBackground

performSelector doesn't work if app in background

protspace
  • 2,047
  • 1
  • 25
  • 30