iOS 推送笔记

    iOS 8,iOS  9 推送做了一些改变,以前的推送内容限制512B,现在扩充到2k吧。而且iOS以后推送注册方式也发生了改变! 主要是下面红色部分!我们要先注册用户通知。
 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.title = @"删除";
        action1.identifier  = @"删除test";
        action1.activationMode = UIUserNotificationActivationModeBackground;
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
        action2.title = @"回复";
        action2.identifier = @"回复test";
        [action2 setBehavior:UIUserNotificationActionBehaviorTextInput];
        action2.activationMode = UIUserNotificationActivationModeBackground;
        
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"alert";//这组动作的唯一标示
        [categorys setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal];
        
       <span style="color:#ff0000;"> UIUserNotificationType type = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
        UIUserNotificationTypeSound;
        
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObject:categorys]];
        [application registerForRemoteNotifications];
        [application registerUserNotificationSettings:settings];</span>
    
    }

注册本地通知:

  UILocalNotification *notification = [[UILocalNotification alloc] init];
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];//本地推送间隔多久时间发生
    notification.fireDate = fireDate;
    notification.timeZone = [NSTimeZone defaultTimeZone];//当前时区
    notification.repeatInterval = kCFCalendarUnitDay;//重复间隔
    notification.alertBody =  @"该起床了...";//通知栏显示的文字
    notification.applicationIconBadgeNumber = 1;//APP数字
    notification.soundName = UILocalNotificationDefaultSoundName;//声音
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
    notification.userInfo = userDict;//参数字典
    //按上面约定时间执行通知
    //[[UIApplication sharedApplication] presentLocalNotificationNow:notification];//立刻执行
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];//按约定时间执行

推送的扩张

        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"action";//按钮的标示
        action.title=@"回复";//按钮的标题
        [action setBehavior:UIUserNotificationActionBehaviorTextInput];//点击这个按钮会弹出输入框
        action.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候启动程序
        //action.authenticationRequired = YES;//是否需要解锁
        action.destructive = YES;//
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2";
        action2.title=@"Reject";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
        action.destructive = YES;
        
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"alert";//这组动作的唯一标示
        [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

        
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:[NSSet setWithObjects:categorys, nil]];

这里当你的本地通知也加入扩展时 

    notification.category = @"alert"; 的标题必须与 categorys.identifier = @"alert";//这组动作的唯一标示 

扩展的操作
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{

    if ([identifier isEqualToString:@"回复test"]) {
        
        NSInteger num = [responseInfo[UIUserNotificationActionResponseTypedTextKey] integerValue];
        
        application.applicationIconBadgeNumber += num;
        
        NSLog(@"回复");
        
    }
    else if([identifier isEqualToString:@"删除test"])
    {
    
        application.applicationIconBadgeNumber = 90;
        
        NSLog(@"删除");
    }
    
    completionHandler();
}

你可以在这里面操作




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值