투케이2K

142. (Objective-C/objc) [iOS 17] UNUserNotificationCenter setBadgeCount 사용해 푸시 알림 뱃지 카운트 초기화 수행 본문

Objective-C

142. (Objective-C/objc) [iOS 17] UNUserNotificationCenter setBadgeCount 사용해 푸시 알림 뱃지 카운트 초기화 수행

투케이2K 2024. 4. 12. 10:19
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

// ------------------------------------------------------------------------------
// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
// ------------------------------------------------------------------------------
- (void)testMain {
    [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
            M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
            description:@"테스트 함수 시작 실시" data:nil];

    /*
     ------------------------------------
     [요약 설명]
     ------------------------------------
     1. 필요 import :
     
     #import <UserNotifications/UNUserNotificationCenter.h>
     ------------------------------------
     2. IOS 17 이상 뱃지 푸시 카운트 초기화 사용
     ------------------------------------
     */

    
    
    // [try catch 구문 정의 실시]
    @try {
        
        // ---------------------------------------------
        // [로직 처리 실시]
        // ---------------------------------------------
        //*
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (@available(iOS 17, *)) {
                UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
                [center setBadgeCount:0 withCompletionHandler:^(NSError * _Nullable error) {
                    if (error != NULL){
                        [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
                                M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
                                description:@"IOS 17 이상 : 뱃지 초기화 에러" data:nil];
                    }
                    else {
                        [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
                                M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
                                description:@"IOS 17 이상 : 뱃지 초기화 성공" data:nil];
                    }
                }];
            }
            else {
                [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
                
                [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
                        M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
                        description:@"IOS 17 미만 : 뱃지 초기화 성공" data:nil];
            }
            
        });
        // */
        // ---------------------------------------------
        /*
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // [10 초 딜레이 실행]
            
        });
        // */
        // ---------------------------------------------

    }
    @catch (NSException *exception) {
        NSLog(@"\n[NSException : 예외 상황 발생] : %s\n", exception.description.UTF8String);
    }
}
 

[결과 출력]


반응형
Comments