IOS 8 provides an exciting new API to create interactive notifications (interactive notifications), which allows you to provide additional functionality to your users outside of your app. I found there is no good example tutorial on how to implement it online, so I will implement a simple interactive notification example to share with you in this article.
To create interactive notifications, you need 3 new classes provided by iOS 8: uiusernotificationsettings, Uiusernotificationcategory, Uiusernotificationaction, and their variants.
You can now register for custom notification categories (categories) and actions (actions), compared to previously simply registering notification types (sounds, banners, alerts). The category describes the type of notification that is applied to the custom and contains the response actions that the user can perform. For example, you receive a notification that someone has focused on you on the social network, and in response you may want to focus on him or ignore it.
Here is a very simple example written using objective-c, demonstrating how to register a notification that contains two actions.
NSString *ConstNotificationcategoryident =@"ACTIONABLE"; NSString*ConstNotificationactiononeident =@"Action_one"; NSString*ConstNotificationactiontwoident =@"Action_two"; - (void) registerfornotification {uimutableusernotificationaction*Action1; Action1=[[Uimutableusernotificationaction alloc] init]; [Action1 Setactivationmode:uiusernotificationactivationmodebackground]; [Action1 settitle:@"Action 1"]; [Action1 setidentifier:notificationactiononeident]; [Action1 Setdestructive:no]; [Action1 Setauthenticationrequired:no]; Uimutableusernotificationaction*Action2; Action2=[[Uimutableusernotificationaction alloc] init]; [Action2 Setactivationmode:uiusernotificationactivationmodebackground]; [Action2 settitle:@"Action 2"]; [Action2 setidentifier:notificationactiontwoident]; [Action2 Setdestructive:no]; [Action2 Setauthenticationrequired:no]; Uimutableusernotificationcategory*actioncategory; Actioncategory=[[Uimutableusernotificationcategory alloc] init]; [Actioncategory setidentifier:notificationcategoryident]; [Actioncategory Setactions:@[action1, Action2] forcontext:uiusernotificationactioncontextdefault]; Nsset*categories =[Nsset setwithobject:actioncategory]; Uiusernotificationtype Types= (uiusernotificationtypealert|Uiusernotificationtypesound|Uiusernotificationtypebadge); Uiusernotificationsettings*settings; Settings=[Uiusernotificationsettings settingsfortypes:types Categories:categor IES]; [[UIApplication sharedapplication] registerusernotificationsettings:settings];}
To send this notification type, simply add the category to the declaration.
" APS " : { "alert""pull down to interact. " " , " category " " ACTIONABLE " }
Now in response to user-selected actions, you need to add two new methods to the Uiapplicationdelegate protocol:
Application:handleActionWithIdentifier:forLocalNotification:completionHandler:application: HandleActionWithIdentifier:forRemoteNotification:completionHandler:
After the user chooses an action from your push notification, the method will be called in the background.
- (void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier forremotenotification :(nsdictionary *) UserInfo Completionhandler: (void(^) ()) Completionhandler {if([identifier isequaltostring:notificationactiononeident]) {NSLog (@"You chose action 1."); } Else if([identifier isequaltostring:notificationactiontwoident]) {NSLog (@"You chose action 2."); } if(Completionhandler) {Completionhandler (); }}
As stated in the documentation, the identifier is chosen to determine which action is selected and the last call to Completionhandler is done. Here is a simple demonstration of iOS 8 new notification API surface features, in the future I will be more in-depth study, have the opportunity to share with you.
IOS 8 Creating interactive notifications