IOS local notification push iOS10.0 new API usage summary, iosios10.0
1. Local notification iOS10.01.1. send push notification
/// ViewController. m // iOS10.0 push notifications locally /// Created by zhouyu on. // Copyright? 2017 zhouyu. All rights reserved. // # import "ViewController. h" # import
@ Interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. title = @ "local notification after ios10.0"; [self setUpUI];} //-(void) touchesBegan :( NSSet
*) Touches withEvent :( UIEvent *) event {// use UNUserNotificationCenter to manage notifications. // UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; ///// create an UNMutableNotificationContent object that contains the content to be notified. Note that this object is not UNNotificationContent and is an immutable object. // UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init]; // content. title = [NSString localizedusernotifstringforkey: @ "Local push Title" arguments: nil]; // content. body = [NSString localizedusernotifstringforkey: @ "Local push Body" arguments: nil]; // content. sound = [UNNotificationSound defaultSound]; ///// push the local push after the set time // UNTimeIntervalNotificationTrigger * trigger = [U NTimeIntervalNotificationTrigger // triggerWithTimeInterval: 5 repeats: NO]; // UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier: @ "FiveSecond" // content: content trigger: trigger]; ///// processing after successful push addition! // [Center addNotificationRequest: request withCompletionHandler: ^ (NSError * _ Nullable error) {// NSLog (@ "Push successful"); //}]; //}-(void) setUpUI {UIButton * btn1 = [[UIButton alloc] initWithFrame: CGRectMake (100,300,150, 60)]; [btn1 setTitle: @ "Jump to the red controller" forState: UIControlStateNormal]; [btn1 setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; [btn1 addTarget: self action: @ selector (red) forContro LEvents: UIControlEventTouchUpInside]; [self. view addSubview: btn1]; UIButton * btn2 = [[UIButton alloc] initWithFrame: CGRectMake (100,400,150, 60)]; [btn2 setTitle: @ "Jump blue controller" forState: UIControlStateNormal]; [btn2 setTitleColor: [UIColor blueColor] forState: UIControlStateNormal]; [btn2 addTarget: self action: @ selector (blue) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: btn2];}- (Void) red {[self pushUNUserNotificationCenterWithRequestIdentifier: @ "red" contentTitle: @ "red controller" contentBody: @ "Jump to red controller"];}-(void) blue {[self pushUNUserNotificationCenterWithRequestIdentifier: @ "blue" contentTitle: @ "blue controller" contentBody: @ "Jump to blue controller"];} (void) pushUNUserNotificationCenterWithRequestIdentifier :( NSString *) identifier contentTitle :( NSString *) title contentBody :( NSString *) body {// use UNUs ErNotificationCenter to manage the notification UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; // you need to create an UNMutableNotificationContent object that contains the content to be notified. Note that this object is not UNNotificationContent and is an unchangeable object. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init]; content. title = [NSString localizedusernotifstringforkey: title arguments: nil]; content. body = [NSString localizedusernotifstringforkey: body arguments: nil]; content. sound = [UNNotificationSound defaultSound]; // push UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTri Gger triggerWithTimeInterval: 5 repeats: NO]; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier: identifier content: content trigger: trigger]; // processing after successful push addition! [Center addNotificationRequest: request withCompletionHandler: ^ (NSError * _ Nullable error) {NSLog (@ "Pushed successfully") ;}] ;}@ end
1.2. register and receive push notifications and perform logic processing
/// AppDelegate. m // iOS10.0 local push notification /// Created by zhouyu on. // Copyright? 2017 zhouyu. All rights reserved. // # import "AppDelegate. h" # import
# Import "ViewController. h" # import "RedController. h" # import "BlueController. h" @ interface AppDelegate ()
@ End @ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; self. window. rootViewController = [[UINavigationController alloc] initWithRootViewController: [[ViewController alloc] init]; [self. window makeKeyAndVisible]; self. window. backgroundCol Or = [UIColor whiteColor]; // use the unusernotifcenter center to manage the notification UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; // listen to the callback Event center. delegate = self; // iOS 10 can be authorized to register using the following methods: [center requestauthorizationwitexceptions :( centers + UNAuthorizationOptionSound + conditions) completionHandler: ^ (BOOL granted, NSError * _ Nullable error ){ NSLog (@ "error = % @", error) ;}]; return YES;}/* typedef NS_OPTIONS (NSUInteger, UNNotificationPresentationOptions) {UNNotificationPresentationOptionBadge = (1 <0 ), UNNotificationPresentationOptionSound = (1 <1), unnotifprespresentationoptionalert = (1 <2),} * // when the application is at the front end, it receives a local notification, how is it presented. The system provides three forms:-(void) userNotificationCenter :( UNUserNotificationCenter *) center willPresentNotification :( UNNotification *) notification withCompletionHandler :( void (^) (unicationprespresentationoptions )) completionHandler {NSLog (@ "% @", notification. request); UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "APP on the frontend" delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", nil]; [alert show]; // use completionHandler to indicate the completionHandler (unicationicationpresentationoptionsound) in the form of a notification displayed on the foreground );} // when the background or program is killed, click the notification bar to call it. It will not be called at the front end-(void) userNotificationCenter :( UNUserNotificationCenter *) center didReceiveNotificationResponse :( unicationresponresponse *) response withCompletionHandler :( void (^) (void) completionHandler {NSLog (@ "% @", response); // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "the APP wakes up on the background or after being killed to the foreground" delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", nil]; // [alert show]; [self jumpToControllerWithUNNotificationResponse: response]; completionHandler () ;}// determine the jump-(void) jumpToControllerWithUNNotificationResponse :( unicationicationresponse *) response {UINavigationController * nav = (UINavigationController *) self. window. rootViewController; if ([response. notification. request. identifier isEqualToString: @ "red"]) {[nav pushViewController: [[RedController alloc] init] animated: YES];} else {[nav pushViewController: [[BlueController alloc] init] animated: YES] ;}}@ end