Simple implementation of local notifications

Local notifications can be sent without being connected to the Internet, and can be used to periodically remind users to do something. Notifications can be sent whether the APP is open or not, but it should be noted that the notification will not be displayed if the APP is in the foreground. The following will briefly organize the implementation of local push notifications.

Sign up for notifications, requesting user authorization

The classification can be set according to your own needs. If you don't need it, you can not set it. The parameters below are directly passed nil

// 注册通知请求授权
- (void)registLocalNotification{
    // 创建分类 - 此处以删除和标记已读为例
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
    category.identifier = @"category";
    // 删除
    UIMutableUserNotificationAction *actionDelete = [[UIMutableUserNotificationAction alloc]init];
    actionDelete.identifier = @"delete";
    actionDelete.title = @"删除";
    actionDelete.activationMode = UIUserNotificationActivationModeForeground;
    actionDelete.authenticationRequired = NO;
    actionDelete.destructive = YES;
    // 标记已读
    UIMutableUserNotificationAction *actionMark = [[UIMutableUserNotificationAction alloc]init];
    actionMark.identifier = @"mark";
    actionMark.title = @"标记已读";
    actionMark.activationMode = UIUserNotificationActivationModeForeground;
    actionMark.authenticationRequired = YES;
    actionMark.destructive = NO;
    /*
     // 自定义输入框 可以在代理方法中获取到输入内容
     UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc]init];
     action.identifier = @"action";
     action.activationMode = UIUserNotificationActivationModeForeground;
     action.behavior = UIUserNotificationActionBehaviorTextInput;
     action.authenticationRequired = YES;
     action.destructive = NO;
     */
    [category setActions:@[actionDelete,actionMark] forContext:UIUserNotificationActionContextDefault];
    // 注册通知 请求授权
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]];
    [[UIApplication sharedApplication]registerUserNotificationSettings:settings];
}

Click button to send notification

Here you can set different notifications through the properties of the local notification object according to your own needs. I just set a few simple properties as a case.

// 点击发送本地通知
- (void)sendLocalNotification{
    // 实例化本地通知对象
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    // 定制本地通知
    // 推送展示时间(此处设置为5s后)
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.applicationIconBadgeNumber = 3;
    localNotification.alertBody = @"本地通知测试";
    // 锁屏时展示滑动解锁内容
    localNotification.alertAction = @"localNotification";
    // 此处的分类标识为前边设置的分类标识
    localNotification.category = @"category";
    // 通知携带参数,代理方法的参数中可以获取到此处传递的参数
    localNotification.userInfo = @{@"paramater" : @"携带测试参数"};
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Handling of notifications received

After receiving the notification, it can be processed by its proxy method in the AppDelegate of the program. The notification information can be obtained in the parameters of the proxy method.
Clicking the received notification banner will call this method.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"接收到本地通知");
}

2 Clicking the custom UIMutableUserNotificationAction in the front category will call the following method

// 点击通知消息动作条调用 iOS9.0之后
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    // 此处可以判断是点击的标记已读还是删除按钮,然后做相应的处理
    if ([identifier isEqualToString:@"delete"]) {
        NSLog(@"delete");
    }else if ([identifier isEqualToString:@"mark"]){
        NSLog(@"mark");
    }else{

    }
    completionHandler();
}
// 点击通知消息动作条调用 iOS9.0之前
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
    // 此处可以判断是点击的标记已读还是删除按钮,然后做相应的处理
    if ([identifier isEqualToString:@"delete"]) {
        NSLog(@"delete");
    }else if ([identifier isEqualToString:@"mark"]){
        NSLog(@"mark");
    }else{

    }
    completionHandler();
}

The receiving notification style is as follows

write picture description here
write picture description here
write picture description here
write picture description here
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325991890&siteId=291194637
Recommended