I'm developing application which acquire the functionalities of Local notification. When few conditions matched, i need to fire Local notifications. I'm done with firing local notification and actions then. But i need to show all fired notifications in 'Notifications Center' even application is foreground or background.
I'm use following code :
In didFinishLaunchingWithOptions method :
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
When few conditions matched :
for (int i = 0; i < [arrayNotif count]; i++) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setAlertBody:[NSString stringWithFormat:#"%#", [[arrayNotif objectAtIndex:i] valueForKey:#"title"]]];
[localNotif setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotif setTimeZone:[NSTimeZone defaultTimeZone]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[arrayNotif objectAtIndex:i] forKey:[NSString stringWithFormat:#"%#", [[arrayNotif objectAtIndex:i] valueForKey:#"id"]]];
[localNotif setUserInfo:dictionary];
localNotif.soundName = UILocalNotificationDefaultSoundName;
[localNotif setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
dictionary = nil;
}
I get all notifications by didReceiveLocalNotification: method only application is in foreground. Now, i want to show all notifications in Notification Center.
Related
I used Notification Alert for my app and used the below code
if(condition){
[defaults setBool:YES forKey:#"notificationIsActive"];
[defaults synchronize];
//self.message.text=#"Notifications Started";
NSTimeInterval interval;
interval = 5;
UILocalNotification* localNotification = [[UILocalNotification
alloc] init];
localNotification.fireDate = [NSDate
dateWithTimeIntervalSinceNow:interval]; //Enter the time here in
seconds.
localNotification.alertBody= #"Alert time";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval=
NSCalendarUnitDay;//NSCalendarUnitMinute;
//Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else
{
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
if ([localNotification.alertBody isEqualToString:#"Alert time"]) {
NSLog(#"the notification this is canceld is %#", localNotification.alertBody);
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
}
Now problem is I got extra notifications showing this is message Users will see. I checked everything that there is no extra notification alert I added. Now what can I do? Please help..
EDIT:
One point is missing. I used that notification for testing in my app but later I removed that notification.
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I'm not receiving local notification,can anyone say how to get local notification.
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
Try this!!!!
You need to specify the time when to fire notification. You have just created notification but not set when it need to be fired.
For example..
localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];
If application is foreground stage then you wan't seen those notification. you should have to code on delegate method.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
UILocalNotification *notif = [[UILocalNotification alloc]init];
notif.alertBody = #"Your Session is Ready";
notif.alertAction = #"Take Now";
notif.soundName = #"DingBell.wav";
[notif setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notif.repeatInterval = NSWeekCalendarUnit;
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableDictionary *userDict = [NSMutableDictionary dictionary];
[userDict setValue:self.stringModuleID forKey:#"kRemindMeNotificationDataKey"];
[userDict setValue:[NSString stringWithFormat:#"alarm%d",[[setreminingDay objectAtIndex:i] intValue]] forKey:#"alarm"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
In iOS7.1 , when a user kills an app , does the geofencing still works?
If geofencing is detecting the device is entering or exiting the region, can it trigger a local notification (even when the user has killed the app ) ?
you GeoFencing still works it works like push notification / or passbook same thing it will still generate notification for your app. bellow function will fire even if app is not running in background.
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:#"didExitRegion %# at %#", region.identifier, [NSDate date]];
[self updateWithEvent:event];
//implement local notification:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (notification == nil)
return;
notification.alertBody = [NSString stringWithFormat:#"Did You Lock Your House?"];
notification.alertAction = #"Lock House";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];
[notification release];
}
I want to cancel a UILocalnotification , when i cancel notification still the notification is being fired . I wanted to know do i have to call any delegate method in appdelegate to cancel notification .the code which i am using is executing correctly .but the notification is getting fired .
Should i use NSNotification center which has removeObserver method to cancel uilocalnotification.
Does UILocalnotification fires notification from the app or from the device.
UPDATE - This is how i am scheduling my notification
-(UILocalNotification *)scheduleNotification :(int)remedyID
{
NSString *descriptionBody;
NSInteger frequency;
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSLog(#"%d",remedyID);
descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyTxtDic"];
frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyFrequency"]intValue];
NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
for (NSDate *fireDate in notificationFireDates)
{
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.fireDate = fireDate;
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody, #"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
nil];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
return notif;
}
Cancelling notification
- (void)cancelNotification:(int)remedyId
{
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(#"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
for (UILocalNotification *notification in notifications)
{
int notifRemedyId = [[notification.userInfo objectForKey:#"kRemindMeNotificationRemedyIDKey"]intValue];
NSLog(#"remedyID : %d",remedyId);
NSLog(#"notifyId : %d",notifRemedyId);
if (remedyId == notifRemedyId) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
NSLog(#"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
}
NSNotification center which has removeObserver method to cancel uilocalnotification.
NSNotificationCenter has nothing to do with UILocalNotification. I'm sorry that they both use "notification" somewhere in their names, but that is just coincidence really.
scheduledLocalNotifications will give you the list of all scheduled notifications and use
- (void)cancelLocalNotification:(UILocalNotification *)notification
or you can cancel them all using:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Edit :
NSString *notifRemedyId = #"notifRemedyId";
UILocalNotification *localnotif=[[UILocalNotification alloc]init];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:#"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,#"notifRemedyId",nil];
localnotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif];
Cancel as :
for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:#"kRemindMeNotificationRemedyIDKey"]) {
if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue]) {
[[UIApplication sharedApplication] cancelLocalNotification:aNotif];
break;
}
}
}
Hope it helps you.
I've created a old uilocalnotification, now, I want to update it, I searched on Google but there are no way to update the local notification. So, I decided to cancel it and create a new local notication:
- (void) cancelLocalNotificationByUserInfo: (NSDictionary *)dictInfo
{
UIApplication *application = [UIApplication sharedApplication];
NSArray *notifArr = [application scheduledLocalNotifications];
for (int i=0; i<notifArr.count; i++)
{
UILocalNotification* theNotif = (UILocalNotification *)[notifArr objectAtIndex:i];
if ([theNotif.userInfo isEqual:dictInfo])
{
[application cancelLocalNotification:theNotif];
}
}
}
- (void) addLocalNotification: (NSDate *) fireDate soundName: (NSString *) soundName
alertBody: (NSString *) alertBody infoDict: (NSDictionary *)infoDict
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = alertBody;
// Set the action button
localNotif.alertAction = #"Show me";
localNotif.hasAction = YES;
localNotif.applicationIconBadgeNumber = 1;
localNotif.soundName = soundName;
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
But, everytime, I execute the addLocalNotification method, the old local notification is fired immediately. Please help me!