-->

Local Notification

2019-08-29 10:58发布

问题:

I implemented a local notification in my game app that fires up once a day for a daily bonus. It all works fine when I tap the notification banner, however, when i get in to the app from the app icon, the local notification doesn't work like it should.

Here is my code:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
.......
.......
application.applicationIconBadgeNumber = 0;
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
     NSLog(@"recieved notification %@",localNotif);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Daily Bonus"
                                                   message:@"You recieved 100 free coins"
                                                  delegate:nil
                                         cancelButtonTitle:nil
                                         otherButtonTitles:@"ok", nil];
    [alert show];
    [alert release];
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    float balance = [standardUserDefaults floatForKey:kCurrentScore];
    balance +=100.0f;
    NSLog(@"%g",balance);
    [standardUserDefaults setFloat:balance forKey:kCurrentScore];
    [standardUserDefaults synchronize];
}

I would appreciate any help.

回答1:

That's the way it works.
Starting the app from it's icon will not trigger any notifications to your app, only from the banner.
You should probably use the same logic as the one triggering your notification if you want to reward your users even though they didn't tap the banner - just calculate how long has it been since the app was last ran and do your stuff there.