Different points of iOS8
If you run the above program on IOS8, you will get the following error
Preview 01-Local push notification [mastering][615:7847] attempting to schedule a local notification {fire date = Monday, July, + at 9:02:25 AM Chi Na standard Time, time zone = (null), repeat interval = 0, repeat count = Uilocalnotificationinfiniterepeatcount, Next fir E date = Monday, July for 9:02:25 AM China Standard Time, user info = {Pagekey = friend;}} With a alert but haven ' t received permission from the user to display alerts
That is, on the iOS8 to send local notifications need to request user permissions How to request user rights? Generally in the new version of the change, in the header file will have a corresponding description, so click to Schedulelocalnotification: Method, See if we need information.
Click to go in, we see
This means: After iOS8.0, you need to use UIApplication's Object method registerusenotificationsetting before scheduling the notification: To request authorization from the user.
The code to request permission is generally placed in the Didfinishlaunchingwithoptions: method, in the case of the user does not uninstall, only need to request once, the next time you run the request!
// 1.如果是iOS8请求用户权限 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) { /* UIUserNotificationType: UIUserNotificationTypeBadge = 1 << 0, // 接收到通知可更改程序的应用图标 UIUserNotificationTypeSound = 1 << 1, // 接收到通知可播放声音 UIUserNotificationTypeAlert = 1 << 2, // 接收到通知课提示内容 如果你需要使用多个类型,可以使用 "|" 来连接 */// 向用户请求通知权限// categories暂时传入nil UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [application registerUserNotificationSettings:setting]; }
Run the program
Test click Notifications, go to the app, no problem
Next, let's talk about the categories in-[uiusernotificationsettings settingsfortypes:categories:]
- categories can let us send a notification before a predefined notification that the button can be displayed on the notification, he needs to be an object with an object containing the Uiusernotificationcategory class Nsset. But the official recommendation is that we use its subclass uimutableusernotificationcategory to dynamically add notifications to the behavior of buttons, IOS8 supports both foreground and background behavior.
- notifies the action button to display with a bar;
-
Notifies the action button to display Alertview;
-
Register classifications and add different behaviors to the taxonomy because of the large amount of registered user notification Settings Code we implement a new method registerusernotification
-(void) registerusernotification{//Request notification permissions to users/* Uiusernotificationtype: Type of user notification Uiusernotificationtyp Ebadge = 1 << 0,//Receive notification can change the app icon Uiusernotificationtypesound = 1 << 1,//receive notification to play sound Uiusernoti Ficationtypealert = 1 << 2,//Receive notification lesson prompt content If you need to use more than one type, you can use "|" to connect *///1. Set the user notification permission type Uiusernotificatio NType types = uiusernotificationtypebadge| uiusernotificationtypesound| uiusernotificationtypealert;//2. Create a Notification behavior button//2.1 Create the first behavior uimutableusernotificationaction *action1 = [[Uimutableuserno Tificationaction Alloc] init];//2.1.1 Set the behavior of the unique label action1.identifier = uimutableusernotificationactionbackground;//2. 1.2 Set the caption of the notification button Action1.title = @ "backstage";//What mode to run the application//Uiusernotificationactivationmodeforeground,//when applied in the foreground When triggered//uiusernotificationactivationmodebackground//Even if the application is not in the foreground also triggers Action1.activationmode = Uiusernotificationact ivationmodebackground;//2.1.3 If the lock screen is only visible action1.authenticationrequired = no;//2.1.4 Button Properties Action1.destructive = no;//2.1 Create first behavior uimutableusernotificationaction *action2 = [[Uimu Tableusernotificationaction Alloc] init];//2.1.1 Set the behavior of the unique label Action2.identifier = uimutableusernotificationactionforeground;//2.1.2 Set the caption of the notification button Action2.title = @ "foreground";//What mode to run the application//UI Usernotificationactivationmodeforeground,//When the application is in the foreground trigger//uiusernotificationactivationmodebackground//Even if the app does not In the foreground also trigger Action2.activationmode = Uiusernotificationactivationmodeforeground; 2.1.3 User must enter a password to execute action2.authenticationrequired = YES; The nature of the 2.1.4 button (no effect) action2.destructive = yes;//3. Create user notification classifications Uimutableusernotificationcategory *category = [Uimut Ableusernotificationcategory Alloc] init];//3.1 sets the unique identity of the category Category.identifier = @ "mycategory";//3.2 Sets the notification button// Context://Uiusernotificationactioncontextdefault,//default context (scenario) in English (usually)//Uiusernotificationac Tioncontextminimal//notification content area is limited in the case ofcapacity [Category Setactions:@[action1,action2] forcontext:uiusernotificationactioncontextdefault];//4. Create user notification settings information UIU Sernotificationsettings *setting = [uiusernotificationsettings settingsfortypes:types categories:[NSSet setwithobject:category]];//registration Settings [[UIApplication sharedapplication] registerusernotificationsettings:setting];}
- Specify the classification of notifications when sending local push notifications
// 9.设置通知的类别 ln.category = @"myCategory";
- Monitor the behavior of the Click Notification button, implement the Listening Notification button click Method in Appdelegate
/** * 当用户点击通知上定制的按钮执行的行为(注意:不点击行为按钮,不会进入该方法) * * @param application 应用 * @param identifier 行为标识符 * @param notification 本地通知 * @param completionHandler 完成回调 */- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{// 处理不同行为 if ([identifier isEqualToString:UIMutableUserNotificationActionBackground]) { NSLog(@"后台运行程序"); }else if ([identifier isEqualToString:UIMutableUserNotificationActionForeground]){ NSLog(@"前台运行程序"); }else{ NSLog(@"其他"); } /** You should call the completion handler as soon as you‘ve finished handling the action. 当任务处理完毕时候,你应该尽快的调用completion的block. */// 在当任务完成的时候,调用任务完成的block completionHandler();}
Local push notifications are different on iOS8