美文网首页
iOS友盟推送远程消息的各种处理方法

iOS友盟推送远程消息的各种处理方法

作者: JohnayXiao | 来源:发表于2018-11-11 14:35 被阅读170次

这个是处理app在关闭状态下点击推送消息启动app时的场景,开发文档上好像没有写,自己研究出来的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.  
    if (launchOptions) {
        
//        UIAlertView *alertView;
//        self.alertView = [[UIAlertView alloc]initWithTitle:@"launchOptions" message:launchOptions.descriptionInStringsFileFormat delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//        [self.alertView show];

        [self handleLaunchOptionsRemoteNotification:launchOptions];
    }
    
    return YES;
}

//处理启动app时的远程推送
- (void)handleLaunchOptionsRemoteNotification:(NSDictionary *)userInfo {
    
    NSString *webStr = [[userInfo objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"webUrl"];
    
    if (webStr) {
        
        if ([webStr containsString:@"http"]) {
            
            [self.window.rootViewController presentViewController:[webViewController initWithURL:webStr] animated:YES completion:nil];
            
        }else {
            
            [self.window.rootViewController presentViewController:[webViewController initWithURL:[NSString stringWithFormat:@"http://%@.com", webStr]] animated:YES completion:nil];
            
        }
    }
    
}

下面都是友盟推送开发文档上的

//iOS10以下使用这两个方法接收通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [UMessage setAutoAlert:NO];
    if([[[UIDevice currentDevice] systemVersion]intValue] < 10){
        [UMessage didReceiveRemoteNotification:userInfo];
        [self handleRemoteNotification:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [UMessage setAutoAlert:NO];
        //应用处于前台时的远程推送接受
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        
        NSLog(@"xjy前台时的远程推送接受:%@", userInfo);
        
        [self handleRemoteNotification:userInfo];
        
    }else{
        //应用处于前台时的本地推送接受
        NSLog(@"xjy处于前台时的本地推送接受:%@", userInfo);
    }
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //应用处于后台时的远程推送接受
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        NSLog(@"xjy处于后台时的远程推送接受:%@", userInfo);
        [self handleRemoteNotification:userInfo];
        
    }else{
        //应用处于后台时的本地推送接受
        NSLog(@"xjy处于后台时的本地推送接受:%@", userInfo);
    }
}

相关文章

网友评论

      本文标题:iOS友盟推送远程消息的各种处理方法

      本文链接:https://www.haomeiwen.com/subject/stbxfqtx.html