0

I use this code to create local notification:

        UILocalNotification *notification=[[UILocalNotification alloc]init];
        notification.soundName = @"new_friend_request.mp3";
        notification.repeatInterval = 0;
        notification.alertAction = @"Go to";
        notification.userInfo = [NSDictionary dictionaryWithObject:@"actionNotification" forKey:@"actionNotification"];
        notification.alertBody = @"Body";
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
        [notification release];

But after pressing home button, I don't see the notification alert body text on the home screen of iPhone.

At the very beginning I thought, that the notification is not called, but then I used debug and found out, that the notification is called properly.

So, I don't know, why it doesn't work.

Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • You say (in the comments of the answers) that your app is running in the background. You should add this to your question as it is key. Can you show how your applicationDidEnterBackground looks like? – Hjalmar Nov 29 '12 at 14:54

4 Answers4

3

In my mind, the question is how you're calling presentLocalNotification while the app is in the background. I find that if I use NSTimer or dispatch_after to call the method that invokes presentLocalNotification, that actually is not triggered when the app is in the background, but rather are deferred until after the app returns to the foreground. I wonder if the code that you think is executing presentLocalNotification is actually running when you think it is.

For example, in my test, when I replaced the NSTimer-based invocation of your code with the the following use of scheduleLocalNotification, I do get your notification if the app happens to be running in the background. So replace your line that says presentLocalNotification with:

// put all of your code configuring the UILocalNotification here
// but omit the presentLocalNotification, replacing it with the following code

// just for a test, fire the notification in 5 seconds

notification.fireDate = [[NSDate date] dateByAddingTimeInterval:5.0];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

This is normal behavior, since you trigger the notification directly while your app is running the notification gets send to tour app delegate.

The notification center will not handle the notification since you app is already running.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • 1) I don't get this notification in didReceiveLocalNotification of app delegate; 2) My application is running in the background, so I tried to lock the screen, unlock, but that doesn't solved the issue – Paul T. Nov 29 '12 at 11:32
1

You are using the presentLocalNotificationNow method. This will post notification at the current moment at that time your application is running. If the application is running the local notification will not get an alert or sound, as it is directly received by your application.

If you press home button then you wont get any alert or sound.

presentLocalNotificationNow:

Presents a local notification immediately.

- (void)presentLocalNotificationNow:(UILocalNotification *)notification

Parameters

notification

A local notification that the operating system presents for the application immediately, regardless of the value of the notification’s fireDate property.

Applications running in the background state can immediately present local notifications when there are incoming chats, messages, or updates. Because the operating system copies notification, you may release it once you have scheduled it.

Refer UIApplication class reference for more

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 1) I don't get this notification in didReceiveLocalNotification of app delegate; 2) I don't want to get an alert, I want to get the view 320 * 40 with text at the top of the home screen. – Paul T. Nov 29 '12 at 11:36
  • @PaulGalavic: you don't get that view. Because that was similiar to alert and introduced it in ios 5. A new way to present notification. If you want to display the notification on home screen use `- (void)scheduleLocalNotification:(UILocalNotification *)notification` method. That will display the alert or banner when you are not using the application. You can't display it using `presentLocalNotificationNow:` method – Midhun MP Nov 29 '12 at 11:46
0

The problem was that Local notificatons were turn off for my application in iphone settings. Now I still use presentLocalNotification and everything is working.

My application always live in background, because my app uses sockets.

But anyway, sorry for wasting the time of people, who tried to help me, because the problem was due to me.

Paul T.
  • 4,938
  • 7
  • 45
  • 93