实用知识:关于通知里面的 category 使用方法
2016-05-12 21:15
423 查看
#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 */
相关文章推荐
- golang kafka
- Go语言及Web框架Beego环境
- golang环境
- Google Map 形状显示
- Django基础(一)
- Emacs, Vi, Lisp, Logo, 小众语言集中营
- gossip 算法学习总结
- HDU——1397Goldbach's Conjecture(二分查找+素数打表)
- ffmpeg常用命令
- 解决Django生成csv乱码问题
- 玩转Django的POST请求 CSRF
- 运行第一个Go Web框架
- tango_with_django阅读笔记
- Google protobuf
- go中channel简单使用
- Google protobuf安装
- django _meta方法
- GoogleCloudMessage推送的使用三(整个服务器玩)
- GoogleCloudMessage推送的使用二(正式使用走起)
- 装机员万能驱动(oem自定义logo,链接等)发布了!!!