本地推送的设置与取消

 

// 添加本地通知

-(void)addLocalNotificationWithTime:(NSString *)time{


    NSDateFormatter *formatter = [[NSDateFormatteralloc] init];


    [formatter setDateFormat:@"HH:mm:ss"];


    NSDate *now = [formatterdateFromString:time];//触发通知的时间


    UILocalNotification *noti = [[UILocalNotificationalloc] init];


    if (noti) {

        //设置推送时间

        noti.fireDate = now;//=now


        //设置时区

        noti.timeZone = [NSTimeZonedefaultTimeZone];


        //设置重复间隔,年,月,日,星期,都可以

        noti.repeatInterval =kCFCalendarUnitDay;


        //推送声音

        noti.soundName =UILocalNotificationDefaultSoundName;

        

        //内容

        NSDictionary *infoDic =nil;

        noti.alertBody =@"揽收报表更新了,快去看看吧!";

        NSString *key = [NSStringstringWithFormat:@"lanshou_%@",time];         

        infoDic = @{key:@"揽收报表更新提醒"};


        //显示在icon上的红色圈中的数子

        noti.applicationIconBadgeNumber = 1;

        //[UIApplication sharedApplication].applicationIconBadgeNumber += 1;//这是控制系统图标的数字,设置后就会显示的


        //设置userinfo方便在之后需要撤销的时候使用

        noti.userInfo = infoDic;


        //添加推送到uiapplication

        //iOS8之后需要添加这个才能授权注册

        UIUserNotificationType type =UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;

        UIUserNotificationSettings* settings = [UIUserNotificationSettingssettingsForTypes:typecategories:nil];

        [[UIApplicationsharedApplication] registerUserNotificationSettings:settings];

        

        UIApplication *app = [UIApplicationsharedApplication];


        [app scheduleLocalNotification:noti];

    }

}


//取消本地通知

-(void)cancelLocalNotificationWithKey:(NSString *)key {

    // 获取所有本地通知数组

    NSArray *localNotifications = [UIApplicationsharedApplication].scheduledLocalNotifications;

    

    for (UILocalNotification *notificationin localNotifications) {

        NSDictionary *userInfo = notification.userInfo;

        if (userInfo) {

           // 根据设置通知参数时指定的key来获取通知参数

            NSString *info = userInfo[key];

            

            // 如果找到需要取消的通知,则取消

            if (info !=nil) {

                NSLog(@"取消了定时器%@",key);

                [[UIApplicationsharedApplication] cancelLocalNotification:notification];

                break;

            }

        }

    }

}


 //取消所有通知的

    [[UIApplicationsharedApplication]cancelAllLocalNotifications];



//在app delegate中写假如推送的时候,程序正在运行就会走这个里面

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

    

        UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"请在1小时内揽收订单"

                                                        message:notification.alertBody

                                                       delegate:nil

                                              cancelButtonTitle:@"好的"

                                              otherButtonTitles:nil];

        [alert show];

        //这里,你就可以通过notificationuseinfo,干一些你想做的事情了

        application.applicationIconBadgeNumber -=1;

        

//不需要的时候可以直接取消这个推送

        [[UIApplicationsharedApplication] cancelLocalNotification:notification];

    


}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值