本地推送 UILocalNotification

iOS本地推送UILocalNotification很方便的实现日程提醒,生日提醒等功能,不需要服务器在本地就可以代码实现。iOS8之后需要代码注册否则不能发送信息!

1.本地推送注册

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"action";//按钮的标示
        action.title=@"Accept";//按钮的标题
        action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        //    action.authenticationRequired = YES;
        //    action.destructive = YES;
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2";
        action2.title=@"Reject";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action.authenticationRequired = YES;//需要解锁才能处理,如
        action2.activationMode = UIUserNotificationActivationModeForeground;
        
        action.destructive = YES;
        //2.创建动作(按钮)的类别集合
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"alert";//这组动作的唯一标示
        [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
        
        //3.创建UIUserNotificationSettings,并设置消息的显示类类型
        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notiSettings];
        
    }

2.本地发送信息

UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.alertBody = @"测试推送的快捷回复";//alertBody是设备收到本地通知时横额或锁屏时的主要文字内容
    notification.alertAction = @"现在开启神秘之旅吧!";//alertActions是锁屏时显示的slide to后面的文字内容
    notification.hasAction = YES;//是否显示额外的按钮,为no时alertAction消失
    notification.category = @"alert";
    notification.repeatInterval = kCFCalendarUnitSecond;//循环的周期
    notification.soundName = UILocalNotificationDefaultSoundName;
    /**自定义通知声:直接加至项目里面即可notification.soundName = @“要播放的文件名”;
       注意:1.音频文件时长需要小于30秒的。
            2.格式一定要支持播放,常用的格式caf
     **/
    
    notification.userInfo=[[NSDictionary alloc] initWithObjectsAndKeys:@"ww",@"ee", nil];
    
    notification.applicationIconBadgeNumber = 1;// 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];

3.删除本地推送

// 取出全部本地通知
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    // 设置要移除的通知id
    NSString *notificationId = @"ee";
    
    // 遍历进行移除
    for (UILocalNotification *localNotification in notifications) {
        
        // 将每个通知的id取出来进行对比
        if ([[localNotification.userInfo objectForKey:@"ww"] isEqualToString:notificationId]) {
            
            // 移除某一个通知
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
//            [[UIApplication sharedApplication] cancelAllLocalNotifications];删除所有
        }
    }

4.推送处理

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification

{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app"
                          
                                                    message:notification.alertBody
                          
                                                   delegate:nil
                          
                                          cancelButtonTitle:@"确定"
                          
                                          otherButtonTitles:nil];
    
    [alert show];
    
    //这里,你就可以通过notification的useinfo,干一些你想做的事情了
    
    application.applicationIconBadgeNumber -= 1;
    
}

代码下载: 点击打开链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值