Not Background方法调用LocalNotification

时间:2015-07-09 12:20:12

标签: ios objective-c iphone

如果我从背景强制关闭我的应用程序。然后本地通知来了。如果点击本地通知我的方法在应用程序在前台运行时没有调用。我在iOS中更新。请帮助。

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Reh" object:nil];

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UIApplicationState state = [application applicationState];

    if (state == UIApplicationStateActive) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"

                                                        message:notification.alertBody

                                                       delegate:self cancelButtonTitle:@"OK"

                                              otherButtonTitles:nil,nil];





        [alert show];


        NSLog(@"%@",notification.soundName);






        // AudioServicesPlaySystemSound (1010);




        MyNotificationViewController *profile=[[MyNotificationViewController alloc]initWithNibName:@"MyNotificationViewController" bundle:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshFoeByPush" object:nil];

        self.viewController = [[SWRevealViewController alloc] initWithRearViewController:self.leftMenuController frontViewController:profile];        self.viewController.rightViewController=nil;
        [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionCurveEaseInOut
                        animations:^{self.window.rootViewController = self.viewController;} completion:nil];






        application.applicationIconBadgeNumber = 0;

    }
    else

    {
        NSString *tokend=     [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];
        if (tokend == (id)[NSNull null] || tokend.length == 0 )
        {

        }
        else
        {
            MyNotificationViewController *profile=[[MyNotificationViewController alloc]initWithNibName:@"MyNotificationViewController" bundle:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshFoeByPush" object:nil];

            self.viewController = [[SWRevealViewController alloc] initWithRearViewController:self.leftMenuController frontViewController:profile];        self.viewController.rightViewController=nil;
            [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionCurveEaseInOut
                            animations:^{self.window.rootViewController = self.viewController;} completion:nil];


        }
    }

}

2 个答案:

答案 0 :(得分:2)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification != nil) {
        [self showLocalNotificationAlert:localNotification];
    }

    return YES;
}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{    
    [self showLocalNotificationAlert:notification];
}


-(void)showLocalNotificationAlert:(UILocalNotification *)notification {
    // handle here what you want
}

ALSO

当调用本地通知触发didReceiveLocalNotification方法而不是handleActionWithIdentifier

是的,请将您的内容放在常用方法-(void)showLocalNotificationAlert:(UILocalNotification *)notification中,这样您只需要调用

即可
  1. 当app在forground中时,didReceiveLocalNotification会调用此方法。

  2. 当应用不在forground中并且您点击了通知时,此通知对象可以从didFinishLaunchingWithOptions获取

  3. 用于远程通知

    didFinishLaunchingWithOptions

    NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (remoteNotification) {
            [self showRemoteNotificationAlert:remoteNotification];
        }
    
      

    字典包含用于远程通知的有效负载

    并且还提供了远程通知火灾和远程通知的常用方法。

    洛尔

答案 1 :(得分:0)

当你的应用程序被杀死并点击推送通知时,此功能将触发;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

你应该像这样处理它,

UILocalNotification *localNotif = [launchOptionsobjectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
     Parse or Do something
}