• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

执行粗斜体代码的时候出现+[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]]];
}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!