执行粗斜体代码的时候出现+[CATransaction synchronize] called within tra
#import "ViewController.h"
#import
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn_Clear;
@property (weak, nonatomic) IBOutlet UIButton *btn_getAllDN;
@property (weak, nonatomic) IBOutlet UIButton *btn_DeliverN;
@property (strong, nonatomic) UNUserNotificationCenter *center;
@end
@implementation ViewController
@synthesize center;
- (void)viewDidLoad {
[super viewDidLoad];
[self checkAuthorizationStatus];
[_btn_getAllDN addTarget:self
action:@selector(getDeliveredNotifications)
forControlEvents:UIControlEventTouchUpInside];
[_btn_Clear addTarget:self
action:@selector(removeAllNotifications)
forControlEvents:UIControlEventTouchUpInside];
[_btn_DeliverN addTarget:self
action:@selector(pushANotification)
forControlEvents:UIControlEventTouchUpInside];
}
- (BOOL)checkAuthorizationStatus{
center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
__block BOOL result;
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
//NSLog(@"%@",settings);
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined){
//向用户提出权限申请,此处需要根据content中所包含的内容来申请
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert +
UNAuthorizationOptionSound +
UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
result = YES;
}else if(settings.authorizationStatus == UNAuthorizationStatusDenied){
result = NO;
}
}];
return result;
}
- (void)addNotification:(NSString *)ID{
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"%@",[NSDate date]] arguments:nil];
//content.badge = @1;
NSNumber *badgeNum = [[NSNumber alloc] initWithLong:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
content.badge = badgeNum;
content.categoryIdentifier = @"NOTIFICATION";
content.sound = [UNNotificationSound defaultSound];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
NSString *Identifier = nil;
if (ID == nil){
Identifier = @"newNotification";
}else if (ID != nil){
Identifier = ID;
}
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:Identifier
content:content trigger:trigger];
// Schedule the notification.
[center addNotificationRequest:request withCompletionHandler:nil];
[self setNotificationCategory];
}
- (void)setNotificationCategory{
UNNotificationCategory* generalCategory = [UNNotificationCategory
categoryWithIdentifier:@"GENERAL"
actions:@[]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
// Create the custom actions for expired timer notifications
UNNotificationAction* defaultAction = [UNNotificationAction
actionWithIdentifier:@"DEFAULT"
title:@"Ignore"
options:UNNotificationActionOptionDestructive];
UNNotificationAction* checkAction = [UNNotificationAction
actionWithIdentifier:@"CHECK_ACTION"
title:@"Check"
options:UNNotificationActionOptionForeground];//激活前台
// Create the category with the custom actions.
UNNotificationCategory* reactiveCategory = [UNNotificationCategory
categoryWithIdentifier:@"NOTIFICATION"
actions:@[defaultAction,checkAction]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionNone];
// Register the notification categories.
[center setNotificationCategories:[NSSet setWithObjects:generalCategory,reactiveCategory,nil]];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// Update the app interface directly.
[UIApplication sharedApplication].applicationIconBadgeNumber
= (notification.request.content.badge).integerValue;
NSLog(@"Get the notification!");
completionHandler(UNNotificationPresentationOptionAlert);
}
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler{
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"NOTIFICATION"]) {
//Handle the actions
if ([response.actionIdentifier isEqualToString:@"DEFAULT"]){
[UIApplication sharedApplication].applicationIconBadgeNumber = (NSInteger)(response.notification.request.content.badge.floatValue - 1);
}else if ([response.actionIdentifier isEqualToString:@"CHECK_ACTION"]){
[UIApplication sharedApplication].applicationIconBadgeNumber = (NSInteger)(response.notification.request.content.badge.floatValue - 1);
}
}else if([response.notification.request.content.categoryIdentifier isEqualToString:@"GENERAL"]){
}
completionHandler();
}
- (void)getDeliveredNotifications{
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray * _Nonnull notifications) {
NSLog(@"%@",notifications);
}];
}
- (void)removeAllNotifications{
[center removeAllDeliveredNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)pushANotification{
[self addNotification: [NSString stringWithFormat:@"%@",[NSDate date]]];
} |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|