In iOS8, there are new changes to local notifications and remote notifications, and here's a look.
Take a look at the code copied online
Uimutableusernotificationaction *notificationaction1 =[[Uimutableusernotificationaction alloc] init]; Notificationaction1.identifier=@"Accept"; Notificationaction1.title=@"Accept"; Notificationaction1.activationmode=Uiusernotificationactivationmodebackground; Notificationaction1.destructive=NO; Notificationaction1.authenticationrequired=NO; Uimutableusernotificationaction*notificationaction2 =[[Uimutableusernotificationaction alloc] init]; Notificationaction2.identifier=@"Reject"; Notificationaction2.title=@"Reject"; Notificationaction2.activationmode=Uiusernotificationactivationmodebackground; Notificationaction2.destructive=YES; Notificationaction2.authenticationrequired=YES; Uimutableusernotificationaction*notificationaction3 =[[Uimutableusernotificationaction alloc] init]; Notificationaction3.identifier=@"Reply"; Notificationaction3.title=@"Reply"; Notificationaction3.activationmode=Uiusernotificationactivationmodeforeground; Notificationaction3.destructive=NO; Notificationaction3.authenticationrequired=YES; Uimutableusernotificationcategory*notificationcategory =[[Uimutableusernotificationcategory alloc] init]; Notificationcategory.identifier=@"Email"; //[Notificationcategory Setactions:@[notificationaction3] forcontext:uiusernotificationactioncontextdefault];[Notificationcategory Setactions:@[notificationaction1] forcontext:uiusernotificationactioncontextminimal]; Nsset*categories =[Nsset setwithobjects:notificationcategory, Nil]; Uiusernotificationtype NotificationType= Uiusernotificationtypebadge | Uiusernotificationtypesound |Uiusernotificationtypealert; Uiusernotificationsettings*notificationsettings =[uiusernotificationsettings Settingsfortypes:notificationtype categories:categories]; [[UIApplication sharedapplication] registerusernotificationsettings:notificationsettings]; Uilocalnotification* Localnotification =[[Uilocalnotification alloc] init]; Localnotification.firedate= [NSDate Datewithtimeintervalsincenow:Ten]; Localnotification.alertbody=@"Testing"; Localnotification.category=@"Email";//Same as category identifier[[UIApplication sharedapplication] schedulelocalnotification:localnotification];
This is a piece of code that sends a local notification, which involves several classes of this update,
Uimutableusernotificationaction,uimutableusernotificationcategory,uiusernotificationsettings
We need to build these objects sequentially, and finally use the
[[UIApplication sharedapplication] registerusernotificationsettings:notificationsettings]; Register the notification settings of the program with the system.
Note that when the application is in the foreground, the notification arrives and the system does not pop the alert for the program, but calls the corresponding
Didreceivelocalnotification and
Didreceiveremotenotification These 2 agent functions, the notification content to the program itself processing. When the program is not in the foreground, the system will pop alert for the user, prompting the user to do the appropriate action. Over here
Uimutableusernotificationaction represents an action, and the representation on the interface is a button that is called when the button is clicked.
-(void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier Forlocalnotification: (uilocalnotification *) notification Completionhandler: (void (^) ()) Completionhandler or
-(void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier Forremotenotification: (nsdictionary *) userInfo Completionhandler: (void (^) ()) Completionhandler Ns_available_ios (8_ 0);
No more calls
Didreceivelocalnotification and
Didreceiveremotenotification these 2 proxy functions.
In
There is a property in Uimutableusernotificationaction,
Activationmode, when it was
Uiusernotificationactivationmodebackground, the system will choose to execute code in the background, the execution time is limited, I tested 30s, if more than 30s your program is still in the background, then the system will terminate your program, and throws an exception. Exceptions are as follows
<warning>: <bknewprocess:0x17e685f0; -.BBBB; Pid:922; Hostpid:-1>Has active assertions beyond permitted time: {(<bkprocessassertion:0x17d6b4f0>ID: the-2b9d290f-7f21-4dcb-955b-9d80de693382 name:notification Action process: <bknewprocess:0x17e685f0; -.BBBB; Pid:922; Hostpid:-1> permittedbackgroundduration:30.000000Reason:notificationaction owner PID: thepreventsuspend Preventthrottledownui preventidlesleep preventsuspendonsleep,<bkprocessassertion:0x17d6de50>ID: the-707c3b54-51bc-47da-b779-b11888416fe4 name:deliver Message Process: <bknewprocess:0x17e685f0; -.BBBB; Pid:922; Hostpid:-1> permittedbackgroundduration:10.000000Reason:suspend owner PID: thepreventsuspend preventthrottledowncpu Preventthrottledownui preventsuspendonsleep)}
Note that if you do not click the action button, but by clicking on the notification content itself, then the system will throw like iOS7, calling
Didreceivelocalnotification and
Didreceiveremotenotification these 2 proxy functions.
If the program does not start, when the user clicks the action button, the system will first call
Didfinishlaunchingwithoptions start the program, and then call
-(void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier Forlocalnotification: (uilocalnotification *) notification Completionhandler: (void (^) ()) Completionhandler or
-(void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier Forremotenotification: (nsdictionary *) userInfo Completionhandler: (void (^) ()) Completionhandler Ns_available_ios (8_ 0);
To handle the action, which is consistent with the previous click Notification content itself, will start the program before calling the corresponding function.
Notice in IOS8: Notification draft, being collated ....