iOS 消息推送 - 本地推送,兼顾iOS8 categorys 设置

本地推送

1、申请权限,开启通知
2、app 主动向设备的通知中心发送本地消息
3、在设定的触发时间,产生消息推送

iOS 消息推送 - 本地推送,兼顾iOS8 categorys 设置_第1张图片
本地推送.png
  • 1 注册,iOS8以上,注册方法不一样,因为新增了 categorys

// app 启动时
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //3.创建UIUserNotificationSettings,并设置消息的显示类类型
        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];

        [application registerUserNotificationSettings:notiSettings];
        
    }else{
        [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }

    // 1.如果是正常启动应用程序,那么launchOptions参数是null;  2.如果是通过其它方式启动应用程序,那么launchOptions就值;
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        // 当被杀死状态收到本地通知时执行的跳转代码
        NSLog(@"有消息,但是app是,重新启动时");
    }

  • 2 收到推送消息,处理
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    // 如果是应用程序在前台,依然会收到通知,但是收到通知之后不应该跳转
    if (application.applicationState == UIApplicationStateActive) {
        NSLog(@"应用在前台");
    };
    
    if (application.applicationState == UIApplicationStateInactive) {
        // 当应用在后台收到本地通知时执行的跳转代码
        NSLog(@"应用后台点击消息时进入app");
    }
    
    // UIApplicationStateBackground 这个 多次测试,还不知道什么用,请告知。
}
  • 3 脚标处理
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // app 重新激活,脚标清零
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
  • 4 实现发送本地消息
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:3.];
    
    localNote.alertBody = @"吃饭了吗?";
    localNote.alertAction = @"查看具体的消息";
    localNote.hasAction = YES;
    localNote.alertLaunchImage = @"icon";
    localNote.alertTitle = @"标题";
    localNote.soundName = UILocalNotificationDefaultSoundName;
    localNote.applicationIconBadgeNumber = 1;

    localNote.userInfo = @{@"name" : @"张三", @"toName" : @"李四"};
    
    localNote.repeatInterval = NSDayCalendarUnit;

    localNote.category = @"alert";
    
    
    // 3.调度通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
    
  • 5 iOS8 - categorys
        //1.创建消息上面要添加的动作(按钮的形式显示出来)
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"action";//按钮的标示
        action.title=@"进入app";//按钮的标题
        action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        //    action.authenticationRequired = YES;
        //    action.destructive = YES;
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
        action2.identifier = @"action2";
        action2.title=@"知道了";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
        action.destructive = YES;
        
        //2.创建动作(按钮)的类别集合
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分,在使用推送时选择indentifier
        [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextDefault)];
        

然后在注册时候,添加 categories
        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
  • 6 iOS8 - categories 点击回调
// 本地通知 iOS 8 外部点击回调
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {

        if ([identifier isEqualToString:@"action2"]) {
            NSLog(@"action2");
        } else {
            NSLog(@"action");
        }
    
    if (completionHandler) {
        completionHandler();
    }
}

远程推送

简单流程
1 app设备申请消息推送权限,上报苹果远程通知中心注册
2 苹果远程通知中心注册,返回唯一标识码 deviceToken
3 将返回的唯一标识码deviceToken,上报服务端,保存绑定用户
4 服务器指定推送时,使用deviceToken 上报
5 app设备 在联网状态下,保持与苹果远程通知中心注册长连接。根据deviceToken发送推送消息到指定的app设备

6 远程推送,看极光吧。集成方便。

具体的delegate 与本地的类似,categorys设置一样。

iOS 消息推送 - 本地推送,兼顾iOS8 categorys 设置_第2张图片
远程推送.png

demo:https://github.com/JuYiWei/CZ_Demos
1

你可能感兴趣的:(iOS 消息推送 - 本地推送,兼顾iOS8 categorys 设置)