本地推送

iOS开发 iOS10推送必看(基础篇)

https://www.jianshu.com/p/f5337e8f336d

 

https://github.com/b593771943/XLLLocalNotification_OC

https://blog.csdn.net/u010964503/article/details/52230981

 

参考例子2:在信用卡日历里面有

AppDelegate

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
    {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"推送注册成功");
            }
        }];
        [application registerForRemoteNotifications];
    }

 

 



- (IBAction)switchTapped:(id)sender {
    
    UISwitch *switcher = (UISwitch *)sender;
    
    if (switcher.on) {
        
        [self scheduleLocalNotification];
        return;
    }
    [self removeLocalNotification];
}

- (void)scheduleLocalNotification {
    // 10系统以上
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
    {
        // 1.设置触发条件
        NSDateComponents *components = [[NSDateComponents alloc] init];
        //        components.weekday = 1;
        components.hour = 15;
        components.minute = 9;
        UNCalendarNotificationTrigger *timeTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
        
        // 2.创建通知内容
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.body = @"我是小明";
        content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1);
        content.sound = [UNNotificationSound defaultSound];
        NSString *detail = @"其实他是假小明";
        content.userInfo = @{
                             @"detail":detail
                             };
        // 3.通知标识
        NSString *requestIdentifier = [NSString stringWithFormat:@"%lf", [[NSDate date] timeIntervalSince1970]];
        // 4.创建通知请求,将1,2,3添加到请求中
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:timeTrigger];
        // 5.将通知请求添加到通知中心
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            
            if (!error)
            {
                NSLog(@"推送已经添加成功");
            }
        }];
    } else { // iOS10以下
        [SVProgressHUD showImage:NULL_IMAGE status:@"不支持iOS10.0以下的设备"];
    }
}

- (void)removeLocalNotification {
    
    UIApplication *app = [UIApplication sharedApplication];
    app.applicationIconBadgeNumber = 0;
    [app cancelAllLocalNotifications];
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值