转: http://blog.csdn.net/lincsdnnet/article/details/52970747
iOS10 添加本地推送(Local Notification)
新的推送注册机制
[objc]
view plain
copy
#import <UserNotifications/UserNotifications.h> #import "AppDelegate.h" @interface AppDelegate ()<UNUserNotificationCenterDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }]; return YES; } #pragma mark - UNUserNotificationCenterDelegate -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ completionHandler(UNNotificationPresentationOptionAlert); } @end
推送本地通知
[objc]
view plain
copy
+(void)registerNotification:(NSInteger )alerTime{ UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:alerTime repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:cancelAction]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil]; }]; }
iOS 10 以前本地推送通知:
[objc]
view plain
copy
+ (void)registerLocalNotificationInOldWay:(NSInteger)alertTime { UILocalNotification *notification = [[UILocalNotification alloc] init]; NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime]; NSLog(@"fireDate=%@",fireDate); notification.fireDate = fireDate; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.repeatInterval = kCFCalendarUnitSecond; notification.alertBody = @"该起床了..."; notification.applicationIconBadgeNumber = 1; notification.soundName = UILocalNotificationDefaultSoundName; NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"]; notification.userInfo = userDict; if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; notification.repeatInterval = NSCalendarUnitDay; } else { notification.repeatInterval = NSDayCalendarUnit; } [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
效果图
转载请注明原文地址: https://ju.6miu.com/read-26414.html