How to open a specified page by push in iOS

As we all know, the push function is already an indispensable APP function in APP today. Now I will introduce how iOS can open a specified page through push.

First go to the didFinishLaunchingWithOptions method to configure the message, AppDelegate should follow the MPushRegisterDelegate protocol.
@interface AppDelegate () <MPushRegisterDelegate>

configuration message

MPushNotificationConfiguration *configuration =
[[MPushNotificationConfiguration alloc] init]; 

configuration.types = MPushAuthorizationOptionsBadge |
MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;

[MobPush setupNotification:configurationdelegate:self];

MobPush adds a new setting method and adds a second parameter: delegate , set the second parameter delegate to self

+ (void)setupNotification:(MPushNotificationConfiguration
 *)configuration delegate:(id <MPushRegisterDelegate>)delegate;

Then process the received push message and jump to the corresponding page. Here we take Demo as an example. Click the notification to jump to the web page. First, go to push to create a background configuration url =  http://m.mob.com key-value pair. 


* iOS 8 - 9 foreground received notification background click notification

// iOS 8-9 The foreground receives a notification and the background clicks on the notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
 if (application.applicationState == UIApplicationStateActive)
    { // The application is in the foreground
        // It is best to pop up an Alert first, as shown in the picture below, today's headlines, when you are browsing the news and the application is in the foreground, he will pop up an Alert to tell you whether to view the details
    }
    else
    { // app is in background
         // The application clicks the notification in the background and jumps directly to the web page
        NSString *url = userInfo[@"url"];
        if (url)
        {
            UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
            WebViewController *webVC = [[WebViewController alloc] init];
            webVC.url = url;
            [nav pushViewController:webVC animated:YES];
        }
    }
    
    completionHandler(UIBackgroundFetchResultNewData);
}

 After iOS 10 , the method using the MPushRegisterDelegate protocol


// iOS 10 background click notification
- (void)mpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSUInteger options))completionHandler
{
 // The application clicks the notification in the background and jumps directly to the web page
    NSString *url = userInfo[@"url"];
    if (url)
    {
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        WebViewController *webVC = [[WebViewController alloc] init];
        webVC.url = url;
        [nav pushViewController:webVC animated:YES];
    }
}
// iOS 10 foreground received notification
- (void)mpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
{
// same as above 
}
 

The above is the simpler way I organize it~




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324748824&siteId=291194637