PDA

View Full Version : NSDictionary and NSUserNotification Question




DavidBlack
Aug 13, 2013, 06:05 PM
Hi, guys I am almost done building this app I am making but I have this problem, I looked online on how to get a random keys and values from a NSDictionary it works fine also I have NSUserNotification to get the values from the NSDictionary and give the user a text/quotation. Here is my code:
-(void)friendshipTimer:(NSTimer *)friendshipTimer {
NSLog(@"NSUserNotificationCenter fired");

// refer to this for more help :http://www.renssies.nl/2012/02/mountain-lion-the-new-notifications-center/
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:@"friendshipKeys" ofType:@"plist"];
NSDictionary* plisttext = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//Now to generate and display random sentences, you would need to keep a track of all the keys:

NSArray* keys = [plisttext allValues];
//Then select a random key using the index:

int randomIndex = arc4random() % (keys.count);
NSString* key = [keys objectAtIndex:randomIndex];

//Initalize new notification
NSUserNotification *notification = [[NSUserNotification alloc] init];
//Set the title of the notification
[notification setTitle:@"My Title"];
//Set the text of the notification
[notification setInformativeText:[NSString stringWithFormat:@"%@",key]];
//Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound (tri-tone) and a string of a .caf file that is in the bundle (filname and extension)
[notification setSoundName:NSUserNotificationDefaultSoundName];

//Get the default notification center
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
//Scheldule our NSUserNotification
[center scheduleNotification:notification];



}

But when the notification comes up more that one text/quotation comes up at the same time and overlap each other. Can anyone help?



ArtOfWarfare
Aug 13, 2013, 07:29 PM
Hi, guys I am almost done building this app I am making but I have this problem, I looked online on how to get a random keys and values from a NSDictionary it works fine also I have NSUserNotification to get the values from the NSDictionary and give the user a text/quotation. Here is my code:
-(void)friendshipTimer:(NSTimer *)friendshipTimer {
NSLog(@"NSUserNotificationCenter fired");

// refer to this for more help :http://www.renssies.nl/2012/02/mountain-lion-the-new-notifications-center/
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:@"friendshipKeys" ofType:@"plist"];
NSDictionary* plisttext = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//Now to generate and display random sentences, you would need to keep a track of all the keys:

NSArray* keys = [plisttext allValues];
//Then select a random key using the index:

int randomIndex = arc4random() % (keys.count);
NSString* key = [keys objectAtIndex:randomIndex];

//Initalize new notification
NSUserNotification *notification = [[NSUserNotification alloc] init];
//Set the title of the notification
[notification setTitle:@"My Title"];
//Set the text of the notification
[notification setInformativeText:[NSString stringWithFormat:@"%@",key]];
//Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound (tri-tone) and a string of a .caf file that is in the bundle (filname and extension)
[notification setSoundName:NSUserNotificationDefaultSoundName];

//Get the default notification center
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
//Scheldule our NSUserNotification
[center scheduleNotification:notification];



}

But when the notification comes up more that one text/quotation comes up at the same time and overlap each other. Can anyone help?

What is calling this method?

DavidBlack
Aug 13, 2013, 08:08 PM
What is calling this method?


NSDate *d = [NSDate dateWithTimeIntervalSinceNow:myInt2];
timerFriendTimer = [[NSTimer alloc] initWithFireDate: d
interval: myInt2
target: self
selector:@selector (friendshipTimer:)
userInfo:nil repeats:YES];

It's a timer in - (void)userdefaultsKey1or2Changed:(NSString *)keyPath ofObject:(id)target change:(NSDictionary *)change userInfo:(id)userInfo {}

It just detects changes in the NSUserDefaults

ArtOfWarfare
Aug 13, 2013, 09:27 PM
NSDate *d = [NSDate dateWithTimeIntervalSinceNow:myInt2];
timerFriendTimer = [[NSTimer alloc] initWithFireDate: d
interval: myInt2
target: self
selector:@selector (friendshipTimer:)
userInfo:nil repeats:YES];

It's a timer in - (void)userdefaultsKey1or2Changed:(NSString *)keyPath ofObject:(id)target change:(NSDictionary *)change userInfo:(id)userInfo {}

It just detects changes in the NSUserDefaults

What is the value of myInt2? It looks to me like you're generating notifications every myInt2 seconds.

DavidBlack
Aug 13, 2013, 10:06 PM
What is the value of myInt2? It looks to me like you're generating notifications every myInt2 seconds.

My myInt2 is an NSInteger. I am just taking the value of the textbox and getting it's integerValue example if I place the number on in the text box. And then multiply it by 60 convert it to minutes.

myInt2 = [stringTextBoxTabSuccess integerValue] * 60;

I also have a NSLog to know that is the value just to make sure. NSLog(@"string value is %ld",(long)myInt2);

heyadrian
Aug 16, 2013, 11:51 AM
For starters, use notifications with the object reference.. i.e.

NSDictionary *notificationInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"item1", @"keyForItem1", object2, @"keyForObject2", [myObject ReturnValue], "Object3", nil;

[[NSNotificationCenter defaultCenter]postNotificationName:@"myNotification" object:nil userInfo:notificationInfo];


This way an ENTIRE dictionary object gets passed to the notification centre with all bits inside it. SO on the other end... You would:

have an observer added with something like:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"somethingHappened" object:nil];


and in doSomething:


- (void)doSomething:(NSNotification *)notification{

NSDictionary *notificationInfo;
NSObject *myObject;
notificationInfo = [[notification userInfo] objectForKey:@"somethingHappened"]; // <-- that is the dictionary on its way and it telling which notification it was
myObject = [notificationInfo objectForKey:@"whateverItWasThatWasSentHere"];

ArtOfWarfare
Aug 16, 2013, 12:18 PM
He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.

heyadrian
Aug 19, 2013, 12:43 AM
My myInt2 is an NSInteger. I am just taking the value of the textbox and getting it's integerValue example if I place the number on in the text box. And then multiply it by 60 convert it to minutes.

myInt2 = [stringTextBoxTabSuccess integerValue] * 60;

I also have a NSLog to know that is the value just to make sure. NSLog(@"string value is %ld",(long)myInt2);


the interval of NSTimer is in seconds...


NSTimeInterval
Used to specify a time interval, in seconds.

typedef double NSTimeInterval;
Discussion
NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.


So basically:

myInt2 = [stringTextBoxTabSuccess integerValue] * 3600;


denotes an hour. But also note that the timer will fire every hour after it first fires as you have the repeats:YES. If you just want one instance of the timer being fired after 60 minutes then have:

timerFriendTimer = [[NSTimer alloc] initWithFireDate: d
interval: myInt2
target: self
selector:@selector (friendshipTimer:)
userInfo:nil repeats:NO];

----------

He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.

good point :D My bad xD I meant NSNotificationCenter lol xD Joys of getting mixed with stuff I'm doing to get my magic mouse to be less annoying :D