iOS10 adds local push (Local Notification) instances

  • 2020-12-16 06:08:52
  • OfStack

preface

iOS 10 abandoned UILocalNotification (UIKit Framework) this class, adopted new UserNotifications Framework to push notifications, and thus push notification also own label UN (this treatment is not someone else), and 1 series enhancement to push function improvement (two extension and interface experience optimization), it is apple's son, so to push this part function also becomes the focus in the development.

This paper mainly looked at the relevant documents of iOS 10 and sorted out the local push notification under iOS 10. Since it is all code, I don't want to explain more, but just look at the code and comments, and leave a comment if you have any questions.

New Push registration mechanism

Notice of Registration (ES21en. m) :


#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //  use  UNUserNotificationCenter  To manage notifications 
 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 // Listen for callback events 
 center.delegate = self;
 
 //iOS 10  Use the following method to register for authorization 
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
       completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
       }];
 
 // Gets the current notification Settings, UNNotificationSettings  Is a read-only object that cannot be modified directly and can only be obtained by the following method 
 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  
 }];
 return YES;
}

#pragma mark - UNUserNotificationCenterDelegate
// Process the notification before it is displayed, i.e. there is an opportunity to modify the notification before it is displayed. 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 //1.  Processing notice 
 
 //2.  Use when processing is complete  completionHandler  Used to indicate the form of a notification displayed in the foreground 
 completionHandler(UNNotificationPresentationOptionAlert);
}
@end

Push local notifications


// use  UNNotification  Local notifications 
+(void)registerNotification:(NSInteger )alerTime{
 
 //  use  UNUserNotificationCenter  To manage notifications 
 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
 
 // Need to create 1 One that contains the content to be notified  UNMutableNotificationContent  Object, notice no  UNNotificationContent , This object is immutable. 
 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];
 
 //  in  alertTime  After pushing local push 
 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
 triggerWithTimeInterval:alerTime repeats:NO];

 UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
 content:content trigger:trigger];
 
 // Add processing after successful push! 
 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" Local notifications " message:@" Add push successfully " preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@" cancel " style:UIAlertActionStyleCancel handler:nil];
  [alert addAction:cancelAction];
  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
 }];
}

iOS 10 Local Push notifications before:


+ (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {
 // ios8 After that, you need to add this registration in order to be authorized 
 // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
 // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
 // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
 // categories:nil];
 // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 // //  Notice repeated prompt units, may be days, weeks, months 
 // }
 
 UILocalNotification *notification = [[UILocalNotification alloc] init];
 //  Sets the time for triggering notifications 
 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
 NSLog(@"fireDate=%@",fireDate);
 
 notification.fireDate = fireDate;
 //  The time zone 
 notification.timeZone = [NSTimeZone defaultTimeZone];
 //  Set the repetition interval 
 notification.repeatInterval = kCFCalendarUnitSecond;
 
 //  Inform the content 
 notification.alertBody = @" It's time to wake up ...";
 notification.applicationIconBadgeNumber = 1;
 //  The sound played when the notification is triggered 
 notification.soundName = UILocalNotificationDefaultSoundName;
 //  Inform the parameters 
 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@" Begin to learn iOS developed " forKey:@"key"];
 notification.userInfo = userDict;
 
 // ios8 After that, you need to add this registration in order to be authorized 
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                     categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  //  Notice repeated prompt units, may be days, weeks, months 
  notification.repeatInterval = NSCalendarUnitDay;
 } else {
  //  Notice repeated prompt units, may be days, weeks, months 
  notification.repeatInterval = NSDayCalendarUnit;
 }
 
 //  Perform notification registration 
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

This article has been compiled in iOS push tutorial, welcome to learn to read.


Related articles: