iOS local notification userInfo always null - ios

I am trying to schedule a local notification with some custom data attached which I will then read when the user opens the app by clicking on the notification.
I schedule the local notification like this:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:10];
notification.alertBody = #"Hello World!";
notification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
#"123123123123", #"beaconUUID",
[NSNumber numberWithInt:10], #"beaconMajor",
[NSNumber numberWithInt:20], #"beaconMinor",
nil];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Then in the code which handles the notification I do this:
- (void)onAppDidBecomeActive:(NSNotification*)notification
{
NSLog(#"Object = %#", [notification object]);
NSDictionary* userInfo = [notification userInfo];
NSLog(#"UserInfo = %#", userInfo);
NSString* beaconUUID = [userInfo objectForKey:#"beaconUUID"];
NSLog(#"Beacon UUID = %#", beaconUUID);
}
My problem is that when I try to read the userInfo it always returns null. How do I read the userInfo dictionary from a NSNotification object?
The three NSLog calls from the above code sample print the following in the console:
2015-02-04 14:11:52.690 BeaconPlugin[17050:724150] Object = <UIConcreteLocalNotification: 0x7ff1d37199a0>{
fire date = Wednesday, February 4, 2015 at 2:11:51 PM Central European Standard Time,
time zone = (null),
repeat interval = 0,
repeat count = UILocalNotificationInfiniteRepeatCount,
next fire date = (null),
user info = {
beaconMajor = 10;
beaconMinor = 20;
beaconUUID = 123123123123;
}}
2015-02-04 14:11:52.691 BeaconPlugin[17050:724150] UserInfo = (null)
2015-02-04 14:11:52.691 BeaconPlugin[17050:724150] Beacon UUID = (null)
Which does show that the dictionary values are part of the user info. Anybody got any ideas what I am doing wrong?

You scheduled a UILocalNotification but you are retrieving your user info from a NSNotification, this two is not the same.
you should do this:
UILocalNotification *localNotif = [notification object];
NSDictionary *userInfo = [localNotif userInfo];
NSString* beaconUUID = [userInfo objectForKey:#"beaconUUID"];

Its worked for when i receive from didReceiveLocalNotification protocol
in AppDelegate.m file
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
NSLog(#"Object = %#", notification);
NSDictionary* userInfo = [notification userInfo];
NSLog(#"UserInfo = %#", userInfo);
NSString* beaconUUID = [[notification userInfo] objectForKey:#"beaconUUID"];
NSLog(#"Beacon UUID = %#", beaconUUID);
}
see below output console
2015-02-04 19:11:12.937 SampleNO[6676:907] Object = <UIConcreteLocalNotification: 0x769e780>{fire date = Wednesday, February 4, 2015, 7:11:09 PM India Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = {
beaconMajor = 10;
beaconMinor = 20;
beaconUUID = 123123123123;
}}
2015-02-04 19:11:14.621 SampleNO[6676:907] UserInfo = {
beaconMajor = 10;
beaconMinor = 20;
beaconUUID = 123123123123;
}
2015-02-04 19:11:18.086 SampleNO[6676:907] Beacon UUID = 123123123123

Related

UILocalNotification getting called repetitively as the app launches

I don't know what is the problem here but everytime I run my app , local notifications get called repetitively showing fireDate as NULL.I am trying to take care of this issue from the past 5hrs now.I need help!!!
"<UIConcreteLocalNotification: 0x7f872ead7630>{fire date = (null), time zone = (null), repeat interval = NSCalendarUnitDay, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, 7 September 2015 5:32:44 pm India Standard Time, user info = (null)}",
I have done enough research on UILocalNotification to get started with it,but i'm still facing this problem.
-(void)setDate:(NSDate*)myfireDate andTime1InString: (NSString*)time1Str andTime2InString:(NSString*)time2Str andTime3InString:(NSString*)time3Str{
//concatenate myFireDate with all three times one by one
NSString *myFireDateInString = [dateFormatter stringFromDate:myfireDate];
myFireDateInString = [myFireDateInString stringByAppendingString:#" "];
NSString *dateWithTime1InString = [myFireDateInString stringByAppendingString:time1Str];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateWithTime1 = [dateFormatter dateFromString:dateWithTime1InString];
NSString *mySecondFireDateInString;
NSString *dateWithTime2InString;
NSDate *dateWithTime2;
if ([time2Str length] !=0){
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
mySecondFireDateInString = [dateFormatter stringFromDate:myfireDate];
mySecondFireDateInString = [mySecondFireDateInString stringByAppendingString:#" "];
dateWithTime2InString = [mySecondFireDateInString stringByAppendingString:time2Str];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateWithTime2 = [dateFormatter dateFromString:dateWithTime2InString];
}
NSString *myThirdFireDateInString;
NSString *dateWithTime3InString;
NSDate *dateWithTime3;
if ([time3Str length]!=0){
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
myThirdFireDateInString = [dateFormatter stringFromDate:myfireDate];
myThirdFireDateInString = [myThirdFireDateInString stringByAppendingString:#" "];
dateWithTime3InString = [myThirdFireDateInString stringByAppendingString:time3Str];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateWithTime3 = [dateFormatter dateFromString:dateWithTime3InString];
}
NSLog(#"%#",dateWithTime3);
NSLog(#"%#",dateWithTime2);
//block starts here
void(^notificationBlock)(void) = ^{
appDelegate.localNotification1 = [UILocalNotification new];
appDelegate.localNotification1.fireDate = dateWithTime1;
appDelegate.localNotification1.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
if(dateWithTime2 != nil){//Make a new UILocalNotification object
appDelegate.localNotification2 = [UILocalNotification new];
appDelegate.localNotification2.fireDate = dateWithTime2;
appDelegate.localNotification2.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
}
if(dateWithTime3 !=nil){//MAke a new UILocalNotification object
appDelegate.localNotification3 = [UILocalNotification new];
appDelegate.localNotification3.fireDate = dateWithTime3;
appDelegate.localNotification3.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
}
if([_repeatDaysTextField.text isEqualToString:#"Everyday"]){
// appDelegate.localNotification1.alertBody = #"Time to take your medicine";
appDelegate.localNotification1.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
if(dateWithTime2 != nil){
appDelegate.localNotification2.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
appDelegate.localNotification2.alertBody = #"Not2";
NSLog(#"%#",appDelegate.localNotification2);
}
NSLog(#"%#",[NSString stringWithFormat:#"%#",dateWithTime3 ]);
if(dateWithTime3 != nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
}
}
else if([_repeatDaysTextField.text isEqualToString:#"Alternately"]){
}
else if([_repeatDaysTextField.text isEqualToString:#"Weekly"]){
appDelegate.localNotification1.repeatInterval = kCFCalendarUnitWeekday;
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
if(dateWithTime2!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
}
if(dateWithTime3!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
}
}
else if([_repeatDaysTextField.text isEqualToString:#"Bi-Weekly"]){
}
else if([_repeatDaysTextField.text isEqualToString:#"Monthly"]){
appDelegate.localNotification1.repeatInterval = kCFCalendarUnitMonth;
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
if(dateWithTime2!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
}
if(dateWithTime3!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
}
}
else if([_repeatDaysTextField.text isEqualToString:#"Yearly"]){
appDelegate.localNotification1.repeatInterval = kCFCalendarUnitYear;
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
if(dateWithTime2!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
}
if(dateWithTime3!=nil){
[[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
}
}
};
//block ends here
//method to set notification
[self setNotification:notificationBlock];
}
-(void)setNotification:(void(^)(void))setNotificationBlock{
setNotificationBlock();
}
I got the solution.In the method,didReceiveLocalNotification:,I was adding the notification object to the 'scheduledNotifications' array.So every time a notification was scheduled,the same object was being added to the 'scheduledNotifications' array and that was getting fired again and again.
[UIApplication sharedApplication]scheduledNotifications = notification;
NOTE:Ignore this statement in didReceiveLocalNotification:
This is occuring beacuse you are setting
appDelegate.localNotification.repeatInterval = someUnit;
try setting the repeatInterval value to 1 by checking whether the repeatCount is 0 or not.

How do I make UILocalNotification method keep running

I am trying to build a calendar style app that reminds people when certain events are happening the day before they happen.
I am using UILocalNotifications for this.
I have to restart my app if I want the notification to appear.
How can I have this code continuously run regardless if the app is still running or is closed, and display the notification on time?
I was wondering if I had to put this into the applicationDidEnterBackground method to make it work?
Currently my code looks like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString* path = [[NSBundle mainBundle] pathForResource:#"example"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
NSDate *tomorrow = [today dateByAddingTimeInterval:60*60*24*1];
NSString *tomorrowString = [dateFormatter stringFromDate:tomorrow];
for (int i = 0; i < allLinedStrings.count; i++) {
NSString* strsInOneLine = [allLinedStrings objectAtIndex:i];
NSArray* singleStrs = [strsInOneLine componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#";"]];
NSString *date = [singleStrs objectAtIndex:0];
if ([date isEqualToString:tomorrowString]) {
for (int j = 1; j < singleStrs.count; j+=2) {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [NSDate dateWithTimeInterval:60*60*-24 sinceDate:tomorrow];
notification.alertBody = [singleStrs objectAtIndex:j+1];
notification.alertTitle = [singleStrs objectAtIndex:j];
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}
}
}
// Override point for customization after application launch.
return YES;
}
Your app code does not need to be running for your local notification to be displayed. Once your app has called scheduleLocalNotification:, the notification will display whether or not you app is running.
If you app is in the foreground, you will also want to implement application:didReceiveLocalNotification:. If you want your app to respond to being opened by the user interacting with the notification, you will want to implement application:handleActionWithIdentifier:forLocalNotification:completionHandler:
As to the question of where to put the code that schedules the notification, it should go anywhere in your app that knows the event to be scheduled. It only needs to be called once per notification. It can be scheduled far in advance.

UILocalNotification triggered but not scheduled

Hi people I have a problem with my app, where I want to add some basic LocalNotifications, which repeat themselves every week. I want to do this in a method called "scheduleLocalNotificationForItem:", which is called when the doneBarButtonItem is pressed. This all seems to be working so far, because when I log all the scheduled notifications every scheduled notification shows up. But when I use the app, the scheduled notifications get triggered and show up but there are some additional notifications, which I haven't set myself and I can't determinate where they come from, which appear too.
So here's my code:
- (int)scheduleNotifitactionsForItem:(AlarmItem *)item
{
NSArray *reorderdRepeat = [NSArray arrayWithArray:[self transformArray:item.repeat]];
int missedDays = 0;
int scheduledAlarms = 0;
for (int i = 0; i < item.repeat.count; i++) {
if ([[reorderdRepeat objectAtIndex:i] boolValue] == true) {//Problem determinating true values at end of array
NSInteger integerOfDay = i + 1;//reorderRepeat should contain seven items, icrement i bevore adding it to integerOfDay
NSDate *lastAlarmTime = [self getFireDateForDayOfWeek:integerOfDay withTime:item.time];
NSArray *allAlramTimesForDay = [self getFireDatesForTime:lastAlarmTime andCycle:item.cycles];
for (int i = 0; i < allAlramTimesForDay.count; i++) {
NSDate *alarmTime = [allAlramTimesForDay objectAtIndex:i];
UIApplication *application = [UIApplication sharedApplication];
UILocalNotification *notification = [UILocalNotification new];
NSDictionary *userInfo = #{#"index":[NSString stringWithFormat:#"%d",item.notification]};
notification.repeatInterval = NSCalendarUnitWeekday;
notification.alertBody = item.title;
notification.userInfo = userInfo;
notification.fireDate = alarmTime;
notification.soundName = item.sound;
[application scheduleLocalNotification:notification];
scheduledAlarms += 1;
}
} else {
missedDays += 1;
}
}
return scheduledAlarms;
}
Help is appreciated ;)
Your repeatInterval should be NSCalendarUnitWeekOfYear (or old NSWeekCalendarUnit). NSCalendarUnitWeekday (NSWeekdayCalendarUnit) will repeat everyday.

objectForKey: returning null

Hi people I m working on a AlarmApp and I want to schedule some localNotifications, this works so far, but when I try to delete them my problem appears. I'm trying to list all localNotifications and loop throughout their userInfo to determinate which Notification has to be deleted. I've added a dictionary to the userInfo property of the Notification with value = index of alarm im alarmArray and key = "index" and now I'm trying to get backt that index with objectForKey: I've also tried valueForKey: but it doesn't work. When I log the dictionary it said that the dictionary contains one item: { 0 = index; } but when I try to log the valueForKey:#"index" the console gives me back a "null".
So can someone tell me what I'm doing wrong? Here's my code:
- (void)deleteNotificationForKey:(int)key
{
UIApplication *application = [UIApplication sharedApplication];
NSArray *eventArray = [application scheduledLocalNotifications];
for (int i = 0; i < eventArray.count; i++) {
UILocalNotification* notification = [eventArray objectAtIndex:i];
NSDictionary *currentUserInfo = notification.userInfo;
NSString *index = [currentUserInfo objectForKey:#"index"];
NSLog(#"%s:%#",__PRETTY_FUNCTION__,currentUserInfo);
NSLog(#"%s:%#",__PRETTY_FUNCTION__,index);
NSLog(#"%s:%d",__PRETTY_FUNCTION__,key);
if ([index intValue] == key)
{
NSLog(#"%s:%#",__PRETTY_FUNCTION__,notification);
[application cancelLocalNotification:notification];
}
}
}
It should work if you use stringWithFormat:
NSString *index = [NSString stringWithFormat:#"%#", [currentUserInfo valueForKey:#"index"]];
In this situation your dictionary object is #"index" and key is #0, if you try this, it should work
NSString *index = [currentUserInfo objectForKey:#0];
To solve this problem you need to change #0 and #"index" places when you initialize your dictionary

Local Notification Ever Changing Text

I am working on getting local notifications to fire at a time every day (set by the user). I have done this in the past, but just where it was one static message that would get shown every day. I would like for it to take the text for the local notification from a plist file I have made with each row being a quote. Is there a way to fire local notifications, but have it change the text every day?
I have right now:
- (IBAction)scheduleNotification {
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Today's 5 Minutes With God Study Is Now Available";
notif.alertAction = #"Ok";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSInteger index = [scheduleControl selectedSegmentIndex];
switch (index) {
case 0:
notif.repeatInterval = NSDayCalendarUnit;
break;
case 1:
notif.repeatInterval = 0;
break;
}
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"Today's Quote!"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[self.notifications addObject:notif];
[notif release];
}
}
So, how would I get the alertBody to show a different message each day?
You have to create a new notification every time, for every new message.

Resources