iOS click to push to jump to the details page

5090 단어
System API: iOS < 7, 7 <= iOS < 10, iOS >= 10 app status: background running, foreground running, not started

Agents that receive remote pushes on iOS 6 and earlier


- application: didReceiveRemoteNotification;

- application: didReceiveRemoteNotification official documentation If the app is running, the app calls this method to process incoming remote notifications. If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary . The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.

If the application is running, the application calls this method to handle incoming remote notifications. If the application is not running when the remote notification arrives, this method starts the application and provides the start information. Applications will not call this method to handle remote notifications. At this time, you can get the push information through the application:didFinishLaunchingWithOptions: method. Although it is before iOS 6, the usage range is NS_DEPRECATED_IOS(3_0, 10_0). It's just that there are better options after iOS 7, so this seems a bit tasteless. If you want to adapt to iOS 6 and before, you need to implement this

New proxies from iOS 7 (included) to iOS 10 (not included).


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"[XGDemo] receive slient Notification");
    NSLog(@"[XGDemo] userinfo %@", userInfo);
    if (application.applicationState == UIApplicationStateActive) {
        //  
    }else {
        [self pushToVCWithNotificationInfo:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

- application:didReceiveRemoteNotification:fetchCompletionHandler official documentation Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification:method, which is called only when your app is running in the foreground,the system calls this method when your app is running in the foreground or background.  If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.

application:didReceiveRemoteNotification: Called only when the application is running in the foreground, and this method can be called in the foreground, background, or not started. If both proxy methods are implemented, the system will only call application:didReceiveRemoteNotification:fetchCompletionHandler: Because our application is only compatible with iOS 8 and above, so I will not implement it - (void)application: didReceiveRemoteNotification ; after all, there is an updated Proxy, it is simpler not to deal with the situation that the application is not started separately. Of course you can also use - application: didReceiveRemoteNotification;

New proxy for iOS 10 and above


First add in application:didFinishLaunchingWithOptions
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
#endif
Then implement the proxy
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    NSLog(@"[XGDemo] click notification");
    // 
    UNNotificationRequest *request = response.notification.request;
    // 
    UNNotificationContent *content = request.content;
    // 
    NSDictionary *userInfo = content.userInfo;
    [self pushToVCWithNotificationInfo:userInfo];
    completionHandler();
}
- userNotificationCenter: willPresentNotification: withCompletionHandler;   // App  

Jump to details page


- (void)jumpWithNotificationInfo:(NSDictionary *)info {
    //  
    if (!info) {
        return;
    }
    //  
    NSString *type = info[@"type"];
    //  , 
    UIViewController *topView;
    topView = [self topViewController]; //  
    [topView.navigationController popToRootViewControllerAnimated:NO]; //  
    if ([type isEqualToString:@"commentpass"]) { //  
        self.mainVC.selectedIndex = 0;  //  , 
        DiscoveryDetialViewController *VC = [[DiscoveryDetialViewController alloc] init];
        UINavigationController *nav = self.mainVC.childViewControllers[0];
        VC.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:VC animated:YES];
    }
}

좋은 웹페이지 즐겨찾기