IOS8 has a new notification center with a new notification mechanism. When you receive a push at the top of the screen, you only need to pull down to see the quick-action interface, and you don't need to enter the app to operate. On the lock screen, the push item can also be processed quickly. Basically is to let the user try to do not leave the current page under the premise of processing push information, and improve processing efficiency.
The ability to directly interact with SMS, email, calendar, reminders, third-party applications, you can not enter the program to be able to quickly operate, and focus on the things in hand, the user can do the following actions:
- In the notification banner quick reply information, do not enter the SMS program;
- You can reject or accept email invitations directly;
- Reminders can be marked for completion or delay;
- When a third-party app updates the interface, it can quickly operate directly on the app.
A quick reply to the most common text messages, with an example of an ad message below, explains how to use the Quick Reply feature, as follows:
Recently found some information on the Internet, the function of a simple implementation, the specific steps are as follows:
Create steps
In AppDelegate the didFinishLaunchingWithOptions , follow the steps below to add each code snippet:
1. Create the action to be added on the message, displayed as a button
//接受按钮UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];acceptAction.identifier = @"acceptAction";acceptAction.title = @"接受";acceptAction.activationMode = UIUserNotificationActivationModeForeground;//拒绝按钮 UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];rejectAction.identifier = @"rejectAction";rejectAction.title = @"拒绝";rejectAction.activationMode = UIUserNotificationActivationModeBackground;rejectAction.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;rejectAction.destructive = YES;
2. Create a Category collection of actions (buttons)
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];categorys.identifier = @"alert";NSArray *actions = @[acceptAction, rejectAction];[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];
3. Create a uiusernotificationsettings and set the display type of the message
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
4. Registration push
[[UIApplication sharedApplication] registerForRemoteNotifications];[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
When using push, you need to include a specific category field in the package (the field content needs to be defined as consistent), and when the terminal receives it, it can show the above code corresponding to the category Settings button, and the Response button event.
{"aps":{"alert":"测试推送的快捷回复", "sound":"default", "badge": 1, "category":"alert"}}
DescriptionPush数据包之前能带的数据最多为256字节,现在APPLE将该数值放大到2KB。
Button Handling Events
1. Take the local notification as an example of how to handle the following code is to initiate a local notification event:
UILocalNotification *notification = [[UILocalNotification alloc] init];notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];notification.timeZone = [NSTimeZone defaultTimeZone];notification.alertBody = @"测试推送的快捷回复";notification.category = @"alert";[[UIApplication sharedApplication] scheduleLocalNotification:notification];
2. Implement the following delegate method
(1) method of callback after successful registration of Registerusernotificationsettings
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ NSLog(@"%@", notificationSettings);}
(2) Event handling
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容 NSLog(@"%@----%@",identifier,notification); //处理完消息,最后一定要调用这个代码块 completionHandler();}
After running to press shift + command + H , let the program pushed to the background, or press command+L let the simulator lock screen, will see the effect! If the program is back in the background, after receiving the message, the drop-down message will appear the two buttons just added; if it is a lock screen, the left stroke will appear with the two buttons you just added.
Recently studied the official document of the next iOS8, the function is studied, basically the effect has come out. Because remote message push is cumbersome and requires background support, I use local push instead, basically they want to invoke the proxy method similar. Long story short, here's the basic process:
1. Create a message above the action to be added (the form of the button is displayed)
[OBJC]View PlainCopy
- uimutableusernotificationaction *action = [[uimutableusernotificationaction alloc] init];
- Action. identifier = @ "action"; //Button indication
- Action. Title=@ "Accept"; //The caption of the button
- Action. Activationmode = uiusernotificationactivationmodeforeground; //Start the program when clicked
- //action.authenticationrequired = YES;
- //action.destructive = YES;
- uimutableusernotificationaction *action 2 = [[uimutableusernotificationaction alloc] init]; //second button
- Action2. identifier = @ "Action2";
- Action2. title=@ "Reject";
- Action2. Activationmode = uiusernotificationactivationmodebackground; //When clicked does not start the program, processing in the background
- Action. authenticationrequired = YES; //need to be unlocked to process, if Action.activationmode = Uiusernotificationactivationmodeforeground; then this property is ignored;
- Action. Destructive = YES;
2. Create a Category collection of actions (buttons)
[OBJC]View PlainCopy
- uimutableusernotificationcategory *categorys = [[uimutableusernotificationcategory alloc] init];
- Categorys. identifier = @ "alert"; //The only indication of this set of actions
- [Categorys setactions: @[action,action2] forcontext:( Uiusernotificationactioncontextminimal)];
3. Create a uiusernotificationsettings and set the display class type of the message
[OBJC]View PlainCopy
- uiusernotificationsettings *uns = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound) categories: [nsset setwithobjects: Categorys, nil nil]];
4. Registration push
[OBJC]View PlainCopy
- [[UIApplication sharedapplication] registerusernotificationsettings: uns];
- <pre name="code" class="OBJC">[[uiapplication sharedapplication] registerforremotenotifications];
Userrequires call to Registerusernotificationsettings:? Silentinfo.plist uibackgroundmodes array contains remote-notification?? Can use both
Offline push packet with a specific category field (the field content needs to be defined together with the front and back table, must be consistent ), when the phone receives, it can show the above code corresponding to the category Settings button, and Response button events.
Payload Example: {"APS": {"alert": "Incoming", "Sound": "Default", "badge": 1, "category": "Incomingcall"}}
significant changes: the data that can be brought before the offline push packet is up to a maximum of bytes , and Apple now amplifies that value to 2KB. This is supposed to be for IOS8 only.
5. Initiating a local push message
[OBJC]View PlainCopy
- uilocalnotification *notification = [[uilocalnotification alloc] init];
- Notification. firedate=[nsdate datewithtimeintervalsincenow:5];
- Notification. TimeZone=[nstimezone defaulttimezone];
- Notification. alertbody=@ "Quick reply to test push";
- Notification. Category = @ "alert";
- [[UIApplication sharedapplication] schedulelocalnotification: notification];
- //Use these two methods to determine if registration is successful
- //NSLog (@ "currentusernotificationsettings =%@", [[UIApplication sharedapplication] Currentusernotificationsettings]);
- //[[uiapplication sharedapplication] isregisteredforremotenotifications];
6. Dealing with results in APPDELEGATE.M
[OBJC]View PlainCopy
- //local push notification
- -(void) application: (uiapplication *) application Didregisterusernotificationsettings:(uiusernotificationsettings *) Notificationsettings
- {
- //successfully registered Registerusernotificationsettings: After the callback method
- NSLog (@ "%@", notificationsettings);
- }
- -(void) application: (uiapplication *) application Didreceivelocalnotification:(uilocalnotification *) Notification
- {
- //method called after local push message is received
- NSLog (@ "%@", notification);
- }
- -(void) application: (uiapplication *) application Handleactionwithidentifier:(nsstring *) identifier forlocalnotification :( uilocalnotification *) Notification completionhandler:(void (^) ()) Completionhandler
- {
- //In the non-user interface to receive local messages, the drop-down message will have a quick Reply button, click the button after the call method, according to identifier to determine which button to click, notification for the message content
- NSLog (@ "%@----%@", identifier,notification);
- Completionhandler (); //Finish processing the message, and finally make sure to call this code block
- }
- //Remote push notification
- -(void) application: (uiapplication *) application Didregisterforremotenotificationswithdevicetoken:(nsdata *) Devicetoken
- {
- //Registered successfully with APNs, received the returned Devicetoken
- }
- -(void) application: (uiapplication *) application Didfailtoregisterforremotenotificationswitherror:(nserror *) Error
- {
- //Register failed to APNs, return error message
- }
- -(void) application: (uiapplication *) application Didreceiveremotenotification:(nsdictionary *) UserInfo
- {
- //Receive remote push notification messages
- }
- -(void) application: (uiapplication *) application Handleactionwithidentifier:(nsstring *) identifier forremotenotification :( nsdictionary *) userInfo completionhandler:(void (^) ()) Completionhandler
- {
- //When the app is not launched, received the server push message, the drop-down message will have a quick Reply button, click the button after the method called, according to identifier to determine which button to click
- }
After running to press SHIFT + command +h, to push the program to the background, or press Command+l to let the simulator lock screen, you will see the effect!
If the program is back in the background, after receiving the message, the drop-down message will appear the two buttons just added; if it is a lock screen, the left stroke will appear with the two buttons you just added.
The effect is as follows:
IOS Notification Center Quick Reply