本地推送通知


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1. 注册本地推送通知

    

    // 1.1 注册本地推送通知的设置

    /*

     UIUserNotificationTypeNone    = 0,      没有

     UIUserNotificationTypeBadge   = 1 << 0, 可以显示应用图标上的数字

     UIUserNotificationTypeSound   = 1 << 1, 可以播放声音

     UIUserNotificationTypeAlert   = 1 << 2, 可以弹窗

     */


    // 这个就是推送通知面板的类型

    UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];

    // 为我们的版面设置一个唯一标示(到通知发送的时候进行选择)

    category.identifier = @"mycat1";

    

    // 创建面板的动作按钮

    /*

     // 动作的唯一标识

     @property (nullable, nonatomic, copy) NSString *identifier;

     

     // 标题

     @property (nullable, nonatomic, copy) NSString *title;

     

     // 普通按钮

     UIUserNotificationActionBehaviorDefault,

     // 输入框

     UIUserNotificationActionBehaviorTextInput

     @property (nonatomic, assign) UIUserNotificationActionBehavior behavior NS_AVAILABLE_IOS(9_0);

     


     UIUserNotificationActionBehaviorTextInput模式下设置,发送按钮的标题

     @property (nonatomic, copy) NSDictionary *parameters NS_AVAILABLE_IOS(9_0);

     

     // 

     UIUserNotificationActivationModeForeground, // 打开应用

     UIUserNotificationActivationModeBackground  // 不打开应用

     // 激活模式

     @property (nonatomic, assign) UIUserNotificationActivationMode activationMode;

     

     // 是否验证,如果要求验证

     @property (nonatomic, assign, getter=isAuthenticationRequired) BOOL authenticationRequired;

     

     // 红色是YES,蓝色是NO

     @property (nonatomic, assign, getter=isDestructive) BOOL destructive;

     */

    

    UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];

    action1.identifier = @"action1";

    action1.title = @"action one";

    action1.behavior = UIUserNotificationActionBehaviorDefault;

    action1.activationMode = UIUserNotificationActivationModeBackground;

    action1.authenticationRequired = NO;

    action1.destructive = YES;

    

    UIMutableUserNotificationAction *action2 = [UIMutableUserNotificationAction new];

    action2.identifier = @"action2";

    action2.title = @"action two";

    action2.behavior = UIUserNotificationActionBehaviorDefault;

    action2.activationMode = UIUserNotificationActivationModeBackground;

    action2.authenticationRequired = YES;

    action2.destructive = NO;

    

    

    // 动作按钮添加到面板类型中

    /*

     // 默认可以弹出4

     UIUserNotificationActionContextDefault,  // the default context of a notification action

     // 在空间有限的情况下弹出2

     UIUserNotificationActionContextMinimal   // the context of a notification action when space is limited

     */

    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];

    

    // 创建输入面板样式

    UIMutableUserNotificationCategory *inputCat = [UIMutableUserNotificationCategory new];

    inputCat.identifier = @"inputcat";

    

    // 创建输入动作

    UIMutableUserNotificationAction *inputAction = [UIMutableUserNotificationAction new];

    inputAction.identifier = @"inputAction";

    inputAction.title = @"action input";

    inputAction.behavior = UIUserNotificationActionBehaviorTextInput;

    inputAction.activationMode = UIUserNotificationActivationModeBackground;

    inputAction.authenticationRequired = NO;

    inputAction.destructive = NO;

    // 设置按钮

    inputAction.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"回复"};

    

    [inputCat setActions:@[inputAction] forContext:UIUserNotificationActionContextDefault];

    

    

    // categories不知道参数作用

    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert  categories:[NSSet setWithObjects:category,inputCat, nil]];

    

    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

    

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"launchOptions:%@",launchOptions);

    

  // 当本地推送通知死了以后,通过通知打开应用时回调

    if(launchOptions && launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

        UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

//        [launchOptions writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES];

        // 用于检测拿过来的通知是我们抛出通知

        [notification.userInfo writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES];

        

        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

        // 用于检测是否执行了

//        [@"" writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES encoding:NSUTF8StringEncoding error:nil];

    }

    

    

    return YES;

}


// 程序活着状态下,不管你在前台自动回调,在后台点击通知以后回调

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

    NSLog(@"notification:%@",notification);

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    

    [@"" writeToFile:[NSString stringWithFormat:@"%@/didReceiveLocalNotification",path] atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}


// 专门处理本地推送通知面板中的action

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(nonnull UILocalNotification *)notification completionHandler:(nonnull void (^)())completionHandler {

    if([identifier isEqualToString:@"action2"]) {

        NSLog(@"我真坏");

    } else if([identifier isEqualToString:@"action1"]) {

        NSLog(@"你真坏");

    }

    NSLog(@"%s",__func__);

    

    //必须回调一下

    completionHandler();

}

// 专门处理本地推送通知面板中的action(如果这个方法实现了,上面方法将不执行)

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler {

    

    NSLog(@"%s",__func__);

    

    NSLog(@"responseInfo:%@",responseInfo);

    

    //必须回调一下

    completionHandler();

}


ViewController中:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 2.1 创建通知

    UILocalNotification *notification = [UILocalNotification new];

    

    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    notification.alertTitle = @"孙杨是什么职业";

    notification.alertBody = @"孙杨夺冠了";

    notification.hasAction = YES;

    notification.alertAction = @"萌萌哒";

    notification.applicationIconBadgeNumber = 8;

    notification.userInfo = @{@"key":@"haha"};

    

    // 设置推送通知面板类型

    notification.category = @"inputcat";

    

    // 执行通知

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值