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

来源:互联网 发布:windows smb远程漏洞 编辑:程序博客网 时间:2024/06/03 20:22
#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 */
0 0
原创粉丝点击