【问题标题】:Handling multiple push notifications got error处理多个推送通知出错
【发布时间】:2014-05-30 09:47:53
【问题描述】:

当屏幕打开时,我通过自动推送到 detailViewController 来处理推送通知。当有一个或两个推送通知时它工作正常,因为我可以将每个 detailViewController 推到彼此之上并将其弹回。但是,当我必须弹回两个以上的 detailViewController 时,应用程序会崩溃。它只发生在 iOS 7+ 中。

【问题讨论】:

    标签: ios error-handling push-notification


    【解决方案1】:

    设置通知ID

    - (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
       if(app.applicationIconBadgeNumber!=0)
       {
            app.applicationIconBadgeNumber = app.applicationIconBadgeNumber - 1;
        }
    
        NSString *aps =[NSString stringWithFormat:@"%@",[userInfo objectForKey:@"id"]];
    
    
        if (app.applicationState == UIApplicationStateActive)
        {
            NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]] forKeys:@[@"notifyText"]];
            [arrNotificationData addObject:dict];
            [self showNotificationInActiveMode:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]];
        }
    
    
       if ([aps isEqualToString:@"1"])
        {
            [UserDefault setpushnotificationid:[aps intValue]];       
        }
        if ([aps isEqualToString:@"2"])
        {
            [UserDefault setpushnotificationid:[aps intValue]];
        }
        if ([aps isEqualToString:@"3"])
        {
            [UserDefault setpushnotificationid:[aps intValue]];
        }
        if ([aps isEqualToString:@"4"])
        {
            [UserDefault setpushnotificationid:[aps intValue]];
        }if ([aps isEqualToString:@"5"])
        {
            [UserDefault setpushnotificationid:[aps intValue]];
        }
    
    
    }
    

    使用单例类作为基类并检查 ID 以及推送和弹出视图。

    @interface CustomViewController ()
    
    @end
    
    @implementation CustomViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                                selector:@selector(appBecomeActive)
                                                    name:UIApplicationDidBecomeActiveNotification
                                                  object:nil];
    }
    
    -(void)appBecomeActive
    {
    
        int val=[UserDefault getpushnotificationid];
        if(val!=0){
    
            [self checkforpushnotification];
        }
    }
    
    
    -(void)checkforpushnotification
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        int val=[UserDefault getpushnotificationid];
        [UserDefault setpushnotificationid:0];
    
    
        //.: if current view is profile or setting just dismiss it.because they are present
    
        switch (val)
        {
            case 1:
            {
                if( [self checkIfControllerExist:[DealsViewController class]])
                {
                    return;
                }else{
                    detailViewController *VC = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"VCView"];
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
                break;
    }
    }
    

    现在在你的类中使用 bace 类

    @interface detailViewController : CustomViewController
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多