本地推送,推送信息从顶部弹出

#pragma mark 本地通知测试

//在需要发送通知的类中实现下面两个方法

1.//注册一个本地通知

+(void)registerLocalNotification:(NSInteger)alertTime{

    

    UILocalNotification *notifition = [[UILocalNotification alloc]init];

    //设置触发时间

    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];

    

    notifition.fireDate = fireDate;

    //设置时区

    notifition.timeZone = [NSTimeZone defaultTimeZone];

    //设置重复间隔

    notifition.repeatInterval = kCFCalendarUnitSecond;

    //设置通知的内容

    notifition.alertBody = @"该起床了......";

    notifition.alertTitle = @"星期五不起床";

    //通知被触发时播放的声音

    notifition.soundName = UILocalNotificationDefaultSoundName;

    //通知参数

    notifition.userInfo = [NSDictionary dictionaryWithObject:@"好好学习,天天向上" forKey:@"swift"];

    

    //这个方法是iOS8以后增加的

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];

        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

        //通知重复提示单位,可以是天//

        notifition.repeatInterval = NSCalendarUnitDay;

    }else{

        notifition.repeatInterval = NSDayCalendarUnit;

    }

    //执行通知

    [[UIApplication sharedApplication] scheduleLocalNotification:notifition];

}

2.//在需要的时候取消某个推送

+(void) cancelLocalNotifitionWithKey:(NSString *)key{

    //获取本地通知数组

    NSArray *localNotifitions = [UIApplication sharedApplication].scheduledLocalNotifications;

    for (UILocalNotification *notifition in localNotifitions) {

        NSDictionary *userInfo = notifition.userInfo;

        if (userInfo) {

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

            NSString *info = userInfo[@"key"];

            

            if (info != nil) {

                [[UIApplication sharedApplication] cancelLocalNotification:notifition];

                break;

            }

        }

    }

}

@end


在APPDelegae里面处理通知,方法回调

static UIWindow *window_;

//处理通知,方法回调

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

   //获取通知所带数据

    NSString *noMess = [notification.userInfo objectForKey:@"key"];

    

    window_ = [[UIWindow alloc]init];

    window_.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);

    UILabel *label = [[UILabel alloc]init];

    [label sizeToFit];

    label.text = noMess;

    label.textAlignment = NSTextAlignmentCenter;

    window_.windowLevel = UIWindowLevelAlert;   //设置window的优先级,使窗口显示时能够盖住状态栏

    window_.backgroundColor = [UIColor yellowColor];

    window_.hidden = NO;

    [window_ addSubview:label];

   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        window_ = nil;

    });

    //更新徽章数

    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

    badge --;

    badge = badge >= 0? badge :0;

    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    

    [ZHMeViewController cancelLocalNotifitionWithKey:@"key"];

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值