Local Notification doesn't work on iOS5 - iphone

After configuring everything in notification center, which allows the app to display the notification, my app's local notification doesn't fire.
Do you encounter the same problem?
more information:
The same app compiled from the same source code a few days ago, which compiled with XCode 4.1 and iOS 4.3 SDK, everything works well.
In addition, the app compiled with old version XCode and iOS SDK, can work on iOS5, after upgrade.
However, the app which compiled with the same code, but XCode 4.2 and iOS5 SDK doesn't work.
Do you have any ideas?
Or is there any special work for iOS5?
The sample code is like:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if (0 < [oldNotifications count]) {
[app cancelAllLocalNotifications];
}
// Create a new notification
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = NSDayCalendarUnit; //repeat every day
alarm.alertBody = [NSString stringWithFormat:#"alert"];
[app scheduleLocalNotification:alarm];
[alarm release];
}
Thanks,
Michael

In iOS 5, notifications are managed by Notification Center. You have to register your application with the Notification Center (programmatically), or (non-programmatically) go to Settings > Notifications and select appropriate settings i.e. enable Notification Center, select Alert Style, and others.
You can use following piece of code to register your application with Notification Center (programmatically), by putting it in applicationDidFinishLaunching::
// Although Register For Remote Notifications is not required for Local Notifications,
// but in iOS 5's Notifications, we have to register otherwise the system doesn't register/recognize
// the notifications posted from the application. Note that this behavior is not documented
// as of Oct 2011, and it's possible that it's a bug and will be handled in the future releases.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
HTH.

Related

Why are my local notifications not having sound on by default in iOS 7?

My app uses geofencing and sends a notification.
By default, sounds are off in Settings - Notifications for the app.
iOS 7 to be precise.
Does anyone know how to fix this?
Here is the code responsible for this:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:5 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:#"You are near %#. Don't forget to check in!",place.name];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
If the sound is turned off for your app in the Settings app, then your notification will not play any sound. The user does not allow you to do that.
If the sound is not turned off in the Settings app, that code that you posted should work.
Also if the user set the ringer switch off, then there will be no sound played.
Have you set the applicationIconBadgeNumber in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions?
Comment this code, and try again.....I don't why, but I got the same problem. After I commented this line, my app works correctly.

how to notify user in ios, while app is closed/ inbackground

i want to notify the user when a "time" is reached.
for example:
i have an tableview with many rows. this rows are something like reminders where the user can set the date and time of remind.
So in a row the reminder is set to 24 April 12:15.
At this time the App is closed.
So how can i notify the user that this time is reached?
Is there a way to do that without apple push notification service?
Edit 1:
Thanks for answering, here is an example for local notifications:
//This example adds 20 seconds to current time, use it for testing
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *currentDatePlus = [currentDate dateByAddingTimeInterval:20];
localNotification.fireDate = currentDatePlus;
localNotification.alertBody = #"Hallo ayFon User";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
EDIT 2:
//Add this for iOS 5 Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
You have to use the Local Notifications of iOS. They are done in this exact purpose.
You can find documentation here: Local and Push Notification Programming Guide.
You don't have to use Push Notifications - you could use Local Notifications instead. They are similar to Push notifications except that they are generated on the device instead so you don't need a network connection.
This is a fairly common pattern for alarm type apps.

UILocalNotification not showing up in Notification Center when app is in background

I have a GPS app that registers to run in the background. I also show a UILocalNotification when I have finished a process. This correctly shows, and if the app is open, then it also appears in Notification Center (swipe down from top). But, if I call the UILocalNotification when my app is in the background, or the screen is locked, I DO get the notification, but it does NOT show up in Notification Center.
I am correctly registering for notifications in my app delegate (iOS 5 bug workaround):
// Register for notifications
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Calling the notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.alertBody = msg;
localNotif.alertAction = NSLocalizedString(#"View", nil);
localNotif.soundName = #"alert.caf";
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
Is this a bug? Why would it show up in Notification Center only when my app is open, even though the notification is shown to the user, and not other times?
Are you also using push notifications? I believe there is no need to call registerForRemoteNotificationTypes: if you are only using local.
Check Here
From the look of the code you posted, it does not look like the code would run in the background. It looks like you are trying to present a Local Notification when an even occurs in the background. You can not do this. Local Notifications are meant to be scheduled while the app is running to go off at a later time when the app is not running, and then they will trigger even when the app is closed.
You need to use Push Notifications. Check out Apple's documentation here.
EDIT: Is your app enabled for the notification center in settings?
have you added the UIBackgroundModes key to your Info.plist file ?
You need to have the UIBackgroundModes Key set to location.

iOS 4.3, iPhone 4 not playing custom sound with UILocalNotification

There are posts similar to mine, but I have not found one exactly like it. Here is what happens:
In my app I schedule a local notification:
UIApplication* app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
UILocalNotification *localNotif = [[ UILocalNotification alloc ] init];
NSDate *date = [ [[ NSDate alloc] initWithTimeIntervalSinceNow:30 ] autorelease ];
if (localNotif == nil) {
return;
}
localNotif.fireDate = date;
localNotif.alertBody = #"Nearing Return Location!";
localNotif.alertAction = #"Ok";
localNotif.soundName = #"2_bubble_wrap.caf";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Which works just fine in iOS 5. In iOS 4.3 I took care to make sure that the sound was on and loud before testing. Naturally the file is less than 30 seconds duration. When it triggers on the iPhone 4, I got no sound, just the alert box. When I retested with the default sound, it played that with the notification. When I replaced the .caf file name with gibberish, it also played the default sound, so it knew my file was there just did not play it. When I run it in the iOS 4.3 simulator it does play my .caf sound. I am losing it. Any ideas anyone?
Greg
-
Simulator != Device especially when you play media files, as your Mac supports more codecs than iPhones do. Checking that your caf file is encoded the way the iPhone needs it and correcting any issues with encoding may solve the problem.

UILocalNotification iOS5 issue (no alert shown)

Is it only me or did Apple break the UILocalNotification mechanism in iOS5 (GM + public release)? It seemed to work fine in the betas but since GM only the sound plays back, no alert is shown (yes I have checked the Notification Settings and the app is all on ON).
I've also made a small separate project (with a brand new app identifier just to be on the safe side), and tested the UILocalNotification class with some very simple code, on the order of:
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
singleLocalPush.hasAction = YES;
singleLocalPush.alertBody = #"Alert Body";
singleLocalPush.alertAction = #"Alert Action";
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
anyone experience similar problems? This is pretty annoying if your app IS about local notifications :)
Best,
Kacper
You should make sure your app is enabled for notifications-
Navigate to Setting->Notifications and make sure your app is enabled.
My app's local notifications work fine on iOS 5, and it was set to be deployed to 3.1.2.