如何处理ios目标c中的解析通知?

Posted

技术标签:

【中文标题】如何处理ios目标c中的解析通知?【英文标题】:How to handle parse Notification in ios objective c? 【发布时间】:2015-12-23 07:15:36 【问题描述】:

我有一个应用程序,当应用程序处于非活动状态并且我的代码是这样时,我正在发送解析通知? 在我的

AppDelegate.m

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo

    if([app applicationState] == UIApplicationStateInactive)
    
        //Here I've written
       [PFPUSH handlePush:UserInfo];
    

但我需要转到此通知的特定页面:例如,我需要在触发推送通知时打开我的 MUSICViewController。如何从应用程序委托转到该屏幕; 另一个重要的事情是如果应用程序没有启动,我将如何启动应用程序并处理通知。 对于信息,一些数据将在应用程序启动之前从服务器获取。 请帮忙怎么处理?

【问题讨论】:

你有没有查看其他答案,比如这个:***.com/questions/20757362/… @Jack 你用storyboard或者xib来布局? @iosGuru 故事板 试试这个 UIStoryboard mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PushBildirimlerim pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushBildirimlerim"]; self.window.rootViewController = pvc; @Jack 设置您的故事板名称并设置您的视图控制器标识符。 【参考方案1】:

如果你使用故事板

UIViewController *controller = [[[[app keyWindow] rootViewController] storyboard] instantiateViewControllerWithIdentifier:@"MUSICViewController"];

其他

MUSICViewController *viewController = [[MUSICViewController alloc] init];

比展示这个新的 VC

[[[app keyWindow] rootViewController] presentViewController:viewController
                                                           animated:YES
                                                         completion:nil];

如果 [[app keyWindow] rootViewController] 是导航控制器

UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *nav = [[app keyWindow] rootViewController];
[nav pushViewController:nav animated:YES];

【讨论】:

【参考方案2】:

我正在分享我的应用程序代码,你可能从中得到一些想法

- (void)applicationDidReceiveRemoteNotification:(NSDictionary *)userInfo fromState:(UIApplicationState)state


if ([UIApplication userId]) 

    if (state == UIApplicationStateActive)
    
        [[PPAlerts sharedAlerts]showAlertWithType:AlertTypeToast withMessage:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]];
        //[PFPush handlePush:userInfo];
    


    if (!IS_IPHONE_SIMULATOR)
    
        if ([UIApplication userId])
        
            PFInstallation *currentInstallation = [PFInstallation currentInstallation];
            if (currentInstallation.badge >= 1)
            
                long k = currentInstallation.badge-1;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber:k];
                currentInstallation.badge = k;
                [currentInstallation saveInBackground];
            
        
    

    if ([[userInfo objectForKey:@"t"] isEqualToString:@"request"] || [[userInfo objectForKey:@"t"] isEqualToString:@"t"]) 
        self.isRequest = true;
    
    else
    
    self.isRequest = false;
    

    NSLog(@"1-->user");
    if ([UIApplication userId] && [[UIApplication sharedApplication] applicationState] != UIApplicationStateActive)
    
        NSLog(@"2-->inactive");
        if (self.sidePanelViewController==nil) 
            NSLog(@"3-->forcestop");
            LoginViewController *login= [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
            self.isNotifiction=true;

            self.notificationID = [userInfo numberForJson:@"id"];

            [self openMenuViewcontroller:login];
        
        else
        
            NSLog(@"4-->runnigBG");
            if (self.isRequest) 
                self.tabBarController.selectedIndex = 4;

            
            else
            
            NotificationViewController *notif=[[NotificationViewController alloc]initWithNibName:@"NotificationViewController" bundle:nil];
            notif.isParseNotifiction=true;
            self.notificationID = [userInfo numberForJson:@"id"];
            SuperNavigationController *navNoti = [[SuperNavigationController alloc]initWithRootViewController:notif];
            self.sidePanelViewController.centerPanel = navNoti;
            
        
    
    else
    
      //  [[APIRequest request]HomeCount:@"0" completed:nil];
        if (appDelegate.isLogin) 
        [[APIRequest request]NotifictionCount:@"0" completed:nil];
        [[APIRequest request]invitationCount:@"0" completed:nil];
        [[APIRequest request]whislistCount:@"0" completed:nil];
        
        NSLog(@"5-->active");
        if(self.isRequest)
        
            self.isRequest = false;
            if (self.isRequestOpen) 
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
                [[PFInstallation currentInstallation] setBadge:0];
                [[PFInstallation currentInstallation] saveEventually];
            

        
        else if (appDelegate.isNotifiction)
        
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
            [[PFInstallation currentInstallation] setBadge:0];
            [[PFInstallation currentInstallation] saveEventually];
            [[APIRequest request]NotifictionAll:appDelegate.UserID ShowLoader:NO completed:nil];
        
        else
        

        
    




【讨论】:

嘿 Thnaks 但无法理解您的代码。在 didrecieveRemoteNotiicationMethod 的应用程序委托方法中,如果应用程序在后台。我该怎么办,就写这么多吧…… 您的应用程序在后台,然后` if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive)` 编写代码以在这种情况下打开 MUSICViewController

以上是关于如何处理ios目标c中的解析通知?的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序关闭或不活动时,如何处理 iOS 应用程序中的推送通知?

如何处理 UIAutomation 中的文本通知?

如何处理点击推送通知ios

如何处理通知中的待处理意图

如何处理 FCM 通知而不在 IOS 上键入弹出窗口

如何处理不同语言的 iOS 推送通知