首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未为LocalNotification调用后台方法

未为LocalNotification调用后台方法
EN

Stack Overflow用户
提问于 2015-07-09 20:20:12
回答 2查看 89关注 0票数 0

如果我从后台强制关闭我的应用程序。然后本地通知来.And,如果在本地通知上点击,我的方法不会在应用程序在前台运行时调用。我在iOS中是较新的。请帮帮忙。

代码语言:javascript
复制
-(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];


        }
    }

}
EN

回答 2

Stack Overflow用户

发布于 2015-07-09 21:00:53

代码语言:javascript
复制
- (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
}

也是

当本地通知触发时,调用not handleActionWithIdentifier didReceiveLocalNotification方法

是的,把你的东西放在通用方法-(void)showLocalNotificationAlert:(UILocalNotification *)notification中,这样你只需要调用

  1. 当app在前台时,didReceiveLocalNotification这将被调用。
  2. 当app不在前台时,您点击通知,则可以从app中获取此通知对象

用于远程通知

didFinishLaunchingWithOptions

代码语言:javascript
复制
NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotification) {
        [self showRemoteNotificationAlert:remoteNotification];
    }

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

并提出了远程通知火灾和远程通知窃听的通用方法。

LOL

票数 2
EN

Stack Overflow用户

发布于 2015-07-09 21:14:10

当你的应用被杀时,你点击推送通知,这个功能就会触发;

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

你应该这样处理它,

代码语言:javascript
复制
UILocalNotification *localNotif = [launchOptionsobjectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
     Parse or Do something
}    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31317335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档