• iOS开发本地推送


    1、简介

      本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知。

    2、创建UILocalNotification 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        
        UILocalNotification *localNotifi = [UILocalNotification new];
        localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//发送时间
        localNotifi.alertBody = @"这是一条本地推送";//设置提醒内容
        localNotifi.soundName = UILocalNotificationDefaultSoundName;//设置推送声音
        localNotifi.applicationIconBadgeNumber = 100;//设置app右上角图标标记
    //    localNotifi.hasAction = YES;//锁屏时是否显示内容,默认yes 设置提醒按钮文字
    //    localNotifi.alertAction = @"好的";//按钮文字
        localNotifi.timeZone = [NSTimeZone defaultTimeZone];//设置时区 [NSTimeZone defaultTimeZone],跟随手机的时区
        localNotifi.repeatInterval = NSCalendarUnitMinute;//设置重复每隔多久发送一次 最小单位分钟 。0代表不重复,此属性设置了, 那么调度池不会用完释放!需要手动删除通知对象
        localNotifi.repeatCalendar = [NSCalendar calendarWithIdentifier:@"NSCalendarIdentifierChinese"];//设置依赖的日历历法,默认就是跟随系统走,历法不一样每月重复间隔时间也不一样(如农历是30天)
        if (@available(iOS 8.2, *)) {
            localNotifi.alertTitle = @"本地推送呵呵";
        } else {
            // Fallback on earlier versions
        }//设置弹出框标题
        UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];//使用可变子类
        category.identifier = @"分类";//设置标识符,注意与发送通知设置的category标识符一致
        // 设置前台按钮,点击后能使程序回到前台的叫做前台按钮
        UIMutableUserNotificationAction *actionLeft = [UIMutableUserNotificationAction new];
        actionLeft.identifier = @"left";
        actionLeft.activationMode  = UIUserNotificationActivationModeForeground;
        actionLeft.title = @"前台按钮";
        // 设置后台按钮,点击后程序还在后台执行,如QQ的消息
        UIMutableUserNotificationAction *actionRight = [UIMutableUserNotificationAction new];
        actionRight.identifier = @"right";
        actionRight.activationMode = UIUserNotificationActivationModeBackground;
        actionRight.title = @"后台按钮";
        [category setActions:@[actionLeft,actionRight] forContext:UIUserNotificationActionContextDefault];
        NSSet *catogorySet = [NSSet setWithObject:category];
        localNotifi.category = @"分类";
    //    在哪个区域发送通知, 进入这个区域就发送这个通知,可以进来调一次,出去调一次
    //    @property(nullable, nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
    //    @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);区域是否只检测一次
    //    @property(nullable, nonatomic,copy) NSString *alertLaunchImage;设置启动图片
    //    @property(nullable, nonatomic,copy) NSDictionary *userInfo;推送携带参数
    //    @property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);添加下拉快速回复功能
    
        if (iOS8_OR_LATER) {
    //        UIUserNotificationType 枚举:
    //        UIUserNotificationTypeNone    = 0,
    //        UIUserNotificationTypeBadge   = 1 << 0, //图标标记
    //        UIUserNotificationTypeSound   = 1 << 1, //声音
    //        UIUserNotificationTypeAlert   = 1 << 2, //提醒
    //
    //        categories:用于添加下拉快速回复功能
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:catogorySet];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        }
        
    //    调度本地推送通知(调度后在特定时间fireData发出)
    //    [[UIApplication sharedApplication] scheduleLocalNotification:localNotifi];
        
    //    立即发送本地通知
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotifi];
        
    // 处理退出后通知的点击,程序启动后获取通知对象,如果是首次启动还没有发送通知,那第一次通知对象为空,没必要去处理通知
        if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
            UILocalNotification *localNotifi = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
            [self changeLocalNotifi:localNotifi];
        }
        
        return YES;
    }

    3、移除推送通知

    - (void)deleteLocalNotifi{
    //    删除所有通知
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        
    //    删除指定通知(发出过期的推送不在此数组)
        NSArray *notifiArr = [[UIApplication sharedApplication] scheduledLocalNotifications];
        for (UILocalNotification *localNoti in notifiArr) {
            //根据UserInfo的值,来查看这个是否是你想要删除的通知
            if (localNoti.userInfo) {
                [[UIApplication sharedApplication] cancelLocalNotification:localNoti];
            }
        }
    }

    4、就收到推送处理方法

    //处理通知
    - (void)changeLocalNotifi:(UILocalNotification *)localNotifi{
        // 如果在前台直接返回
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            return;
        }
        // 获取通知信息
    //    localNotifi.userInfo
    }
    //接收到本地通知后调用 程序前台或后台的时候才有用,退出无法接收到消息
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
        [self changeLocalNotifi:notification];
    }

    5、前台和后台按钮处理方法

    //前台和后台按钮处理方法
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)(void))completionHandler{
        // 我们可以在这里获取标识符,根据标识符进行判断是前台按钮还是后台按钮还是神马按钮,进行相关逻辑处理(如回复消息)
        NSLog(@"identifier : %@",identifier);
        // 一旦接受必须调用的方法(告诉系统什么时候结束,系统自己对内部进行资源调配)
        completionHandler();
    }

     

    6、远程推送

      https://www.jianshu.com/p/25e267037f6e

  • 相关阅读:
    mkdosfs 安装
    块设备驱动程序-内存盘
    usb驱动程序小结(六)
    usb的hid鼠标键盘报告描述符(五)
    usb输入子系统写程序(三)
    usb输入子系统键盘(四)
    usb描述符简述(二)
    linux usb总线驱动(一)
    linux 触摸屏驱动
    lcd驱动框架
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8309252.html
Copyright © 2020-2023  润新知