iOS 本地通知配置#

iOS 本地通知配置#

一、在AppDelegate里面进行配置#

#import "AppDelegate.h"

@import UserNotifications;

@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end

@implementation AppDelegate

- (BOOL)application:(NSApplication *)application didFinishLaunchingWithOptions:(NSDictionary<NSApplicationLaunchOptionsKey, id> *)launchOptions {
   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
   center.delegate = self;
   [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
                         completionHandler:^(BOOL granted, NSError * _Nullable error) {
       if (granted) {
           NSLog(@"Notification permission granted.");
       } else {
           NSLog(@"Notification permission denied.");
       }
   }];return YES;
}

// Handle notification when app is running
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
   completionHandler(UNAuthorizationOptionAlert + UNAuthorizationOptionSound);
}

@end

二、相关工具类#

  • JobsLocalNotificationModel

    @interface JobsLocalNotificationModel : NSObject
    
    @property(nonatomic,copy)NSString *identifier;
    @property(nonatomic,copy)NSString *title;
    @property(nonatomic,copy)NSString *body;
    @property(nonatomic,strong)UNNotificationSound *sound API_UNAVAILABLE(tvos);
    @property(nonatomic,assign)NSTimeInterval triggerWithTimeInterval;// 时间间隔必须大于0,否则崩溃
    @property(nonatomic,assign)BOOL repeats;
    
    @end
    @implementation JobsLocalNotificationModel
    
    -(NSString *)identifier{
        if (!_identifier) {
            _identifier = @"DemoNotification";
        }return _identifier;
    }
    
    -(NSString *)title{
        if (!_title) {
            _title = JobsInternationalization(@"本地通知");
        }return _title;
    }
    
    -(NSString *)body{
        if (!_body) {
            _body = JobsInternationalization(@"这是一个示例本地通知");
        }return _body;
    }
    
    -(UNNotificationSound *)sound{
        if (!_sound) {
            _sound = UNNotificationSound.defaultSound;
        }return _sound;
    }
    
    -(NSTimeInterval)triggerWithTimeInterval{
        if (_triggerWithTimeInterval <= 0) {
            _triggerWithTimeInterval = 1;
        }return _triggerWithTimeInterval;
    }
    
    @end
  • JobsMakeLocalNotification

    @interface JobsMakeLocalNotification : NSObject
    
    - (void)triggerLocalNotification:(JobsLocalNotificationModel *)localNotificationModel;
    
    @end
    #import "JobsMakeLocalNotification.h"
    
    @interface JobsMakeLocalNotification ()
    
    @end
    
    @implementation JobsMakeLocalNotification
    
    - (void)triggerLocalNotification:(JobsLocalNotificationModel *)localNotificationModel{
        UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
        UNMutableNotificationContent *content = UNMutableNotificationContent.new;
        content.title = localNotificationModel.title;
        content.body = localNotificationModel.body;
        content.sound = localNotificationModel.sound;
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:localNotificationModel.triggerWithTimeInterval
                                                                                                        repeats:localNotificationModel.repeats];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:localNotificationModel.identifier
                                                                              content:content
                                                                              trigger:trigger];
        [center addNotificationRequest:request
                 withCompletionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"Error adding notification: %@", error);
            } else {
                NSLog(@"Notification scheduled.");
            }
        }];
    }
    
    @end

三、相关调用#

  • [JobsMakeLocalNotification.new triggerLocalNotification:JobsLocalNotificationModel.new];