#import "AppDelegate.h"
#import "MyNotificationDefine.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = MyUserNotificationCategoryStyleIdentifier;
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"Action1";
action.activationMode = UIUserNotificationActivationModeBackground;
action.title = @"进入后台";
[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;
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
NSLog(@"%@ : %@", identifier, responseInfo);
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];
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 */