I am using Repeat Local notofications to display alerts to the user. For this i used below code
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
localNotification.alertBody = #"sss";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Using NSNotification Centre i will call the local notification every 60 seconds. It is working fine.
Also in Appdelegate i use the following code:-
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Received");
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RestartProcess" object:self];
}
application.applicationIconBadgeNumber = 0;
}
Also
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
NSLog(#"Yes Notid=fications is predsent");
application.applicationIconBadgeNumber = 0;
}
return YES;
}
My problem is when local notification starts, i pressed on home button then waited to display. After the time period display a notification message.
When i clicked on app icon the process is stopped.After that Local notifications not working.But when i was clicked on Notification Message it will works right.How i can fix the problem.Can any one help me to do this. Thanks in advance..
didReceiveLocalNotification is only called when you hit on notification messaage. If you want to do it in applicationDidBecomeActive you have to do it manually.
You can check badge number count(if notification consists badge) like :
-(void) applicationDidBecomeActive:(UIApplication *)application
{
if(application.applicationIconBadgeNumber >= 1)
{
//Do you stuff here
application.applicationIconBadgeNumber = 0;
}
}
When you press home button:
UIApplicationState state = [application applicationState];
// at this time, state == UIApplicationStateInactive
When you click your application, i think you should implement in this method:
- (void)applicationDidBecomeActive:(UIApplication *)application- (void)applicationWillEnterForeground:(UIApplication *)application
{
UIApplicationState state = [application applicationState];
application.applicationIconBadgeNumber = 0;
//state at this time is UIApplicationStateInactive
if (state == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RestartProcess" object:self];
}
}
Hope this helps you
Related
I am currently doing this, but the notification repeats several times.Please help.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//localNotification.userInfo = #"Hey";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = self.notiname;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
AudioServicesPlaySystemSound(1104);
}
}
I want to update application badge icon when a notification is received. It works fine when app is running (active mode),but having trouble when trying to set it when app is in suspended/background mode. Want to achieve the same without using the 'badge' field in apns dictionary.
Here is what i am doing,
-(void) incrementOneBadge{
NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
numberOfBadges +=1;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}
-(void) decrementOneBadge{ NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
numberOfBadges -=1;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
}
else {
// Push Notification received in the background
[self incrementOneBadge];
}
[self handlePushNotificationsForState:application withDictionary:userInfo];
}
I am using the NSNotifier to create an Alarm Clock that calls a method when the time set is reached. I can get this to work successfully when the App is in the foreground. But when in the background, I can only get a notification to be presented.
Is it possible to call a method using the NSNotifier, when the app is in the background? If so how do I accomplish this?
Here is my code so far:-
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:nil];
}
- (void) ProcessDidComplete:(NSNotification *)pNotification
{
NSString *processData;
processData = #"Processing Data";
}
viewcontroller.m
-(void)addLocalNotification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.repeatInterval = NSMinuteCalendarUnit;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
According to documentation, -application:didReceiveLocalNotification: only gets called when the application is in the foreground.
See documentation for UIApplicationDelegate and handling local and remote notifications.
If the user has alert style set to Banners. They can receive more than 1 notification without them being prompted to clear it. They then go to use their phone, and they have say 3 stored. If the click on the latest one & it opens the App, I want to only clear just this one notification, I also need to go badgeCount--;
How can I achieve it with the code below? (At the moment it's set to clearing all which I don't want...) I've also noticed that sometimes it DOES update the badge number. But if I toggle back to the iOS Home screen, and pull down the notifications menu, it's still there!
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([[userInfo valueForKey:#"aps"] valueForKey:#"alert"] != nil) {
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
if(message != nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Usage Alert"
message:message delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alertView show];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
}
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplication *app = [UIApplication sharedApplication];
NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number
badgeNumber--; // decrement by one
[app setApplicationIconBadgeNumber:badgeNumber]; // set ne badge number
[app cancelLocalNotification:notification]; // cancel the received notification. It will clear the notification from banner alos
}
You can add
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
to your app delegate. This will be called and there you can use
[[UIApplication sharedApplication] cancelLocalNotification:notification];
to remove the specific notification and decrement the badge count.
I want to warn against calling
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:bg_NUM]
from
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
method!
If your scheduled local notification with some badge number, then badge will be set asynchronously some milliseconds after enter to -didReceiveLocalNotification
for sample:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
// ^^^ maybe not reset badge to 0!! ^^^
}
another code:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
sleep(1); //waiting for system is set our scheduled badge
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
// ^^^ most chances for reset badge to 0 ^^^
}
Testing code, screen touches schedule local notifications
and calculate delay before system real set badge:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
UILocalNotification *localNotif = [[[UILocalNotification alloc] init] autorelease];
localNotif.applicationIconBadgeNumber = rand()%100+1;
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
[...]
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
while ([UIApplication sharedApplication].applicationIconBadgeNumber == 0) sleep(0);
NSLog(#"badge set: %d after %f sec.", [UIApplication sharedApplication].applicationIconBadgeNumber, CFAbsoluteTimeGetCurrent()-startTime);
}
output:
badge set: 41 after 0.000839 sec.
badge set: 9 after 0.000754 sec.
badge set: 56 after 0.076026 sec.
badge set: 17 after 0.069889 sec.
badge set: 8 after 0.056245 sec.
badge set: 71 after 0.120729 sec.
badge set: 28 after 0.122720 sec.
badge set: 17 after 0.000758 sec.
this testing in iOS 4.2/4.3/5.0/6.1 on different devices
Be careful when you reset badge number in -didReceiveLocalNotification message!
(this true only for LocalNotification /not remote push/ and only if application is active on
receive moment)
I'm developing an iOS application, and I have to get local notification, BUT in case of application's state is inactive. I successfully get notifications when application is in background state.
So, is it possible to get local notification, when application is inactive?
Or maybe that's possible only by using push notification?
Regards,
Armen
You need to respond to local notifications in two places in your app delegate:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
The first is for when your app was not running - use the launchOptions parameter to check if your app was launched due to a local notification.
The second is for when your app is currently running (active or inactive). You can check if the app is inactive by checking NSApplication's applicationState property in the application:didReceiveLocalNotification: method.
- (void)sendNotification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// notification.repeatInterval = NSDayCalendarUnit;
localNotification.fireDate = vwPicker.date;
localNotification.alertBody = txtAlarmTitle.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.userInfo = #{#"Title": txtAlarmTitle.text};
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self handleNotification:notification application:application];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification)
[self handleNotification:localNotification application:application];
return YES;
}
-(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application
{
NSString *title = [notification.userInfo objectForKey:#"Title"];
[[[UIAlertView alloc]initWithTitle:#"Smart Alarm" message:title delegate:self cancelButtonTitle:#"Answer the Teaser" otherButtonTitles: nil] show];
application.applicationIconBadgeNumber = 0;
}
Sure, just use willResignActiveNotification notification listener as listed
here