IOS中使用本地推送

本地通知是使用UILocalNotification类并不是系统的NSNotificationCenter通知中心。

  • 本地通知的作用和使用场景

本地通知是由IOS系统管理的一个功能,比如某些后台应用做了某项活动需要我们去处理或者已经退出的应用在某个时间提醒我们唤起,如果注册了通知,系统就会在通知触发的时候给我们发信息,由此我们可以通过系统给我们的app添加通知用户的功能。例如闹钟类应用,按时标签类似地功能应用。

  • 本地类UILocalNotification类

    • fireDate//设置系统发送通知的时间(如果是过去的时间或者0就立即发起通知)
    • timeZone //设置时间的时区
    • repeatlnterval//设置周期性通知

      NSCalendarUnit对象是枚举,设定通知的周期
      typedef NS_OPTIONS(NSUInteger,NSCalendarUnit)
      {
          NSCalendarUnitEra = kCFCalendarUnitEra,//世纪
          NSCalendarUnitYear = kCFCalendarUnitYear,//年
          NSCalendarUnitMonth = kCFCalendarUnitMonth,//月
          NSCalendarUnitDay = kCFCalendarUnitDay,//天
          NSCalendarUnitHour = kCFCalendarUnitHour,//小时
          NSCalendarUnitMinute = kCFCalendarUnitMinte,//分钟
          NSCalendarUnitSecond = kCFCalendarUnitSecond,//秒
          NSCalendarUnitWeekday = kCFCalendarUnitWeekday,//工作日
          NSCalendarUnitWeekdayOrdinal = kCKCalendarUnitWeekdayOrdinal,//工作日定序
      }
    • repeatCalendar //设置周期性通知参照的日历表

    • region //在用户进去或者离开某一个区域时发送通知
    • regionTriggersOnce //设置区域检测通知是否重复
    • alertBody // 设置通知主体
    • hasAction //是否隐藏滑动启动按钮
    • alertLanuchImage //设置点击通知后启动的启动图片

    • alertTitle //通知的短标题,为iwatch增加的接口

    • soundName //收到通知时播放的系统声音
    • applicationIconBadgeNumber //设置应用程序icon头标数字
    • userInfo //用户字典可用于通知消息参数
      注意:这个字符串是系统默认的提示音 NSString *const UILocalNotificationDefaultSoundName;
  • 使用流程和注意事项(都适配ios8)

// 设置本地通知
-(void)registerLocalNotification1:(NSInteger)alertTime {
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知的时间
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
    NSLog(@"fireDate=%@",fireDate);

    notification.fireDate = fireDate;
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔
    notification.repeatInterval = kCFCalendarUnitSecond;

    // 通知内容
    notification.alertBody =  [NSString stringWithFormat:@"111111该起床了%@",fireDate];
    notification.applicationIconBadgeNumber =  [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;;
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
    notification.userInfo = userDict;

    // ios8后,需要添加这个注册,才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重复提示的单位,可以是天、周、月
        // notification.repeatInterval = NSCalendarUnitDay;
    } else {
        // 通知重复提示的单位,可以是天、周、月
        // notification.repeatInterval = NSDayCalendarUnit;
    }

    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

//在软件启动时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   // 程序开启时取消之前注册的通知
    //1.通过点击通知栏通知开启app
   // UILocalNotification *notification=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
   //  [self setLocationNocation:notification];
      [[UIApplication sharedApplication]cancelAllLocalNotifications];//如果不是闹钟就一般直接取消以前的
    return YES;
}

//每次清除通知
- (void)applicationDidBecomeActive:(UIApplication *)application {

     [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

//重新设置通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

    [self setLocationNocation:notification];
}
//设置推送的通知
-(void)setLocationNocation:(UILocalNotification *)notification
{
    if (notification)
    {
        [[UIApplication sharedApplication]cancelLocalNotification:notification];
        // 更新显示的徽章个数

        int count =(int)[[[UIApplication sharedApplication]scheduledLocalNotifications] count];
        if(count>0)
        {
            NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0];

            for (int i=0; i<=count-1; i++)
            {
                UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
                notif.applicationIconBadgeNumber=i+1;
                [newarry addObject:notif];
            }
            [[UIApplication sharedApplication] cancelAllLocalNotifications];
            if (newarry.count>0)
            {
                for (int i=0; i<newarry.count; i++)
                {
                    UILocalNotification *notif = [newarry objectAtIndex:i];
                    [[UIApplication sharedApplication] scheduleLocalNotification:notif];
                }
            }
        }
    }
}

其实查了好多资料,对于app通知角标和通知显示逻辑网上处理的比较粗糙,鉴于能力有限,只能一个一个来处理,特别想和腾讯那样处理,如果哪位大神看到能帮助解决我的疑惑,请指教,不胜感激,
想做到的效果:本地通知,如何能做到点击app图标进入app,角标消失,但是通知栏的通知不会被清空,手动清空通知栏的通知如何让app图标上面的角标也消失。说白了就是如何更科学的处理本地通知。谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值