IOS local push notifications, receive notifications, and redirect control summary, ios jump
IOS local push notifications, receive notifications, and redirect control summary.
1. Local notification iOS8.01.1. local notification sending
* ** After iOS8.0, you can use // to locally notify UILocalNotification * locationNo = [[UILocalNotification alloc] init]; // The trigger time locationNo. fireDate = [NSDate dateWithTimeIntervalSinceNow: 10]; // notification content locationNo. alertBody = @ "this is a local notification"; // It is visible after iOS8.2. Generally, if (@ available (iOS 8.2, *) {locationNo is not used. alertTitle = @ "Haha, local push";} // screen lock (the word behind "slide" in the black screen) -- after iOS10.0, there is no sliding unlocking function, this attribute does not exist in locationNo. alertAction = @ "view local push"; locationNo. hasAction = YES; // The Voice locationNo when the notification comes. soundName = UILocalNotificationDefaultSoundName; // application icon prompt -- the default value is 0, which is not changed. If it is set to a negative number, the locationNo will disappear when the notification arrives. applicationIconBadgeNumber =-1; // directly set the application icon to 0, indicating that the application is hidden, which is consistent with the above [UIApplication sharedApplication]. applicationIconBadgeNumber = 0; // MARK: Extra information -- not important to users, but important to developers. It is used to process the corresponding logic locationNo when receiving notifications. userInfo = @ {@ "name": @ "Zhou Yu", @ "job": @ "iOS development engineer "}; // Add to the scheduling pool --- multiple notifications may exist --- depending on the trigger time [[UIApplication sharedApplication] scheduleLocalNotification: locationNo];
1.2. Receive local notifications
// # Import "AppDelegate. h "register notification-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {/* UIUserNotificationTypeNone = 0, secret badge flag uiusernotiftypetypesound UIUserNotificationTypeAlert pop-up effect * // register a notification // set uiusernotificationicationsettings * setting = [UIUserNotificationSettings failed: Required | uiusernotiftypesound | inclucategories: nil]; [[UIApplication sharedApplication] registerUserNotificationSettings: setting]; // when the program is killed, the notification is received and executed. -- UILocalNotification * notification = launchOptions [uiapplicationlaunchoptionslocalnotificationicationkey]; if (notification) {NSLog (@ "localNo = % @", notification. userInfo); // NSLog will not be printed. // use the test to test whether the notification UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100,100)]; label. backgroundColor = [UIColor redColor]; [self. window. rootViewController. view addSubview: label]; return YES ;}
// When the program is not killed, it is called when a local notification is received -- this method is executed when the notification is clicked. If the program is killed, this method is no longer used. In application: didfinishlaunchingwitexceptions: obtain-(void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {NSLog (@ "notification. userInfo = % @ ", notification. userInfo );
// Lock screen (in the black screen, "slide to")-after iOS10.0, there is no sliding unlocking function, and this attribute does not exist.
LocationNo. alertAction = @ "view local push ";
LocationNo. hasAction = YES;
2. Handle local notification logic iOS8.02.1. send push notifications
// Send the notification // ViewController. m // push locally /// Created by zhouyu on. // Copyright? 2017 zhouyu. all rights reserved. // # import "ViewController. h "# import" RedController. h "# import" BlueController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. title = @ "Local push"; [self setUpUI];}-(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) forControlEvents: 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 pushlocationnotifnotifwithalertbody: @ "Jump to the red controller" userInfo: @ {@ "key": @ "red"}];} -(void) blue {[self pushLocationNotificationWithAlertBody: @ "Jump blue controller" userInfo :@{@ "key": @ "blue"}];}-(void) pushLocationNotificationWithAlertBody :( NSString *) body userInfo :( NSDictionary *) userInfo {// local notification UILocalNotification * locationNo = [[UILocalNotification alloc] init]; // The triggered time locationNo. fireDate = [NSDate dateWithTimeIntervalSinceNow: 5]; // notification content locationNo. alertBody = body; // The Voice locationNo when the notification comes. soundName = UILocalNotificationDefaultSoundName; // application icon prompt -- the default value is 0, which is not changed. If it is set to a negative number, the locationNo will disappear when the notification arrives. applicationIconBadgeNumber =-1; // directly set the application icon to 0, indicating that the application is hidden, which is consistent with the above [UIApplication sharedApplication]. applicationIconBadgeNumber = 0; // MARK: Additional information-not important to users, but important to developers: locationNo. userInfo = userInfo; // Add to the scheduling pool --- multiple notifications may exist --- depending on the trigger time [[UIApplication sharedApplication] scheduleLocalNotification: locationNo];} @ end
2.2. AppDelegate receives push notifications for logic processing
// Receive the notification and perform logic processing // AppDelegate. m // push locally /// Created by zhouyu on. // Copyright? 2017 zhouyu. all rights reserved. // # import "AppDelegate. h "# import" ViewController. h "# import" RedController. h "# import" BlueController. h "@ interface AppDelegate () @ end @ implementation AppDelegate-(BOOL) application :( UIApplication *) application didFinishLaunchingWithOptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; self. window. rootViewController = [[UINavigationController alloc] initWithRootViewController: [[ViewController alloc] init]; [self. window makeKeyAndVisible]; self. window. backgroundColor = [UIColor whiteColor];/* required = 0, uiusernotiftypetypebadge badge marked uiusernotiftypetypesound sound effect pop-up * // registration notification // local notification settings UIUserNotificationSettings * setting = [UIUserNotificationSettings settings failed: UIUserNotificationTypeBadge | uiusernotiftypetypesound | UIUserNotificationTypeAlert categories: nil]; [[UIApplication sharedApplication] registerUserNotificationSettings: setting]; // when the program is killed, how to receive and execute the notification -- discard the notification after ios10.0, You need to test UILocalNotification * notification = launchOptions [UIApplicationLaunchOptionsLocalNotificationKey] In versions earlier than 10.0; if (notification) {NSLog (@ "localNo = % @", notification. userInfo); // NSLog will not print UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100,100)]; label. backgroundColor = [UIColor redColor]; [self. window. rootViewController. view addSubview: label]; [self jumpToControllerWithLocationNotification: notification];} return YES;} // when the program is not killed, this method is called when a local notification is received-this method is executed when the notification is clicked. If the program is killed, this method will not go any longer, and the method will be obtained in application: didfinishlaunchingwitexceptions: (void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {NSLog (@ "notification. userInfo = % @ ", notification. userInfo); [self jumpToControllerWithLocationNotification: notification];}-(void) jumpToControllerWithLocationNotification :( UILocalNotification *) localNo {// If the APP is on the frontend, the notification method is not required. if ([UIApplication sharedApplication]. applicationState = UIApplicationStateActive) {return;} // obtain userInfo NSDictionary * userInfo = localNo. userInfo; UINavigationController * nav = (UINavigationController *) self. window. rootViewController; // determine the jump if ([userInfo [@ "key"] isEqualToString: @ "red"]) {[nav pushViewController: [[RedController alloc] init] animated: YES];} else if ([userInfo [@ "key"] isEqualToString: @ "blue"]) {[nav pushViewController: [[BlueController alloc] init] animated: YES] ;}}@ end
// If the APP is on the frontend, the notification method is not required.
If ([UIApplication sharedApplication]. applicationState = UIApplicationStateActive ){
Return;
}