0

I have an app that receives push notification. I would like to add the badge with the right value near a button inside the application menu (do you remember the old Facebook app?).

I'm trying to get the badge value from the notification in the AppDelegate, save it in NSUserDefault to use it in other view controllers.

NSString * badgeValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:badgeValue forKey:@"badge"];
[defaults synchronize];

The problem is that if I try to put the value in a label the app crashes

'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance

Xcode makes me save badgeValue in a string but when I put the string in the label it says is not a proper string

if ([badgeValue isKindOfClass:[NSString class]]) {
    NSLog(@"it is string !!!!!!!!!!!!!!!");
    // treat it as a string object
} else {
     NSLog(@"it is not a string !!!!!!!!!!!!!!!");
    // treat it as a number object
}

By debugging I see that badgeValue is not a real string but __NSCFNumber and if I try to convert it in a string the return value is a long strange number. I searched everywhere for a way to get the badge value of the notification but the only option I can think of is a php query...Any idea?

Miwi
  • 774
  • 4
  • 15
  • 28

2 Answers2

0

What do you mean by "Xcode makes me save badgeValue in a string"? Do you get an error or a compiler warning if you try to type badgeValue as an NSNumber? The documentation and your own logging tell you that the value of the key "badge" is an NSNumber.

NSNumber * badgeValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
[label1 setIntValue:badgeNumber.intValue];

Does something like this not work?

rdelmar
  • 103,982
  • 12
  • 207
  • 218
0

Yes, Just yesterday I tried with

 NSString *string = [NSString stringWithFormat:@"%d", [badgeValue intValue]];

And it works! Thanks for help

Miwi
  • 774
  • 4
  • 15
  • 28