实用知识:关于通知里面的 category 使用方法

本文介绍如何在iOS应用中定制用户通知的样式与行为,包括创建可交互的通知动作,并演示了如何在代码中实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "AppDelegate.h"
#import "MyNotificationDefine.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


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

    /*================= UIMutableUserNotificationCategory =================*/
    // categories 配置 通知的类别, (有多少种样式的通知)
    // 通知弹窗的样式, 必须先注册, 才能使用
    // 注意: 使用Mutable类型的category
    // 1. 实例化
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    // identifier是唯一标识, 通知会使用该ID来匹配相关的样式
    category.identifier = MyUserNotificationCategoryStyleIdentifier;


    /*================= UIMutableUserNotificationAction =================*/
    // category上的按钮
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    // action的identifier用来在代理当中区分是哪个按钮触发
    action.identifier = @"Action1";

    // 配置相关参数
    /**
     UIUserNotificationActivationModeForeground, // 点击通知后, 会跳转到App当中, 也会触发代理方法
     UIUserNotificationActivationModeBackground  //  点击了通知后, 不会跳转到App当中, 但是会触发代理方法
     */
    action.activationMode = UIUserNotificationActivationModeBackground;
    action.title = @"进入后台";

    // 将action添加到category当中
    [category setActions:@[action] forContext:UIUserNotificationActionContextDefault];

    /*================= 注册配置 =================*/
    NSSet *set = [NSSet setWithObject:category];
    // 注册通知的配置 (包含授权)
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:set];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    return YES;
}

// iOS8.0后
//- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
//{
//    
//}

// iOS9.0后
// 如果实现了该方法, 上面失效
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
    NSLog(@"%@ : %@", identifier, responseInfo);

//    if (identifier isEqualToString:@"Action1") {
//        做相关处理
//    }

    // 当执行完Action的相关代码时, 注意要执行CompletionHandler
    completionHandler();
}


@end
#import "ViewController.h"
#import "MyNotificationDefine.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 发个通知

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

    // 先注册, 再通过id来使用
    // category 是通知所使用的显示样式的category的identifier
    notification.category = MyUserNotificationCategoryStyleIdentifier;

    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4];
    notification.alertBody = @"不要放弃治疗";

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

@end
#ifndef MyNotificationDefine_h
#define MyNotificationDefine_h

#define MyUserNotificationCategoryStyleIdentifier @"MyUserNotificationCategoryStyleIdentifier"

#endif /* MyNotificationDefine_h */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值