IOS Notification Center Quick Reply

Source: Internet
Author: User

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
  1. uimutableusernotificationaction  *action = [[uimutableusernotificationaction alloc] init];
  2. Action. identifier = @ "action"; //Button indication   
  3. Action. Title=@ "Accept"; //The caption of the button   
  4. Action. Activationmode = uiusernotificationactivationmodeforeground; //Start the program when clicked   
  5. //action.authenticationrequired = YES;   
  6. //action.destructive = YES;   
  7. uimutableusernotificationaction *action 2  = [[uimutableusernotificationaction alloc] init]; //second button
  8. Action2. identifier = @ "Action2";
  9. Action2. title=@ "Reject";
  10. Action2. Activationmode = uiusernotificationactivationmodebackground; //When clicked does not start the program, processing in the background   
  11. Action. authenticationrequired = YES; //need to be unlocked to process, if Action.activationmode = Uiusernotificationactivationmodeforeground; then this property is ignored;   
  12. Action. Destructive = YES;

2. Create a Category collection of actions (buttons)

[OBJC]View PlainCopy
    1. uimutableusernotificationcategory  *categorys = [[uimutableusernotificationcategory alloc] init];
    2. Categorys. identifier = @ "alert"; //The only indication of this set of actions   
    3. [Categorys setactions: @[action,action2] forcontext:(  Uiusernotificationactioncontextminimal)];

3. Create a uiusernotificationsettings and set the display class type of the message
[OBJC]View PlainCopy
    1. uiusernotificationsettings *uns = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound) categories: [nsset setwithobjects:  Categorys, nil nil]];

4. Registration push

[OBJC]View PlainCopy
    1. [[UIApplication sharedapplication] registerusernotificationsettings: uns];
    2. <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
  1. uilocalnotification  *notification = [[uilocalnotification alloc] init];
  2. Notification. firedate=[nsdate datewithtimeintervalsincenow:5];
  3. Notification. TimeZone=[nstimezone defaulttimezone];
  4. Notification. alertbody=@ "Quick reply to test push";
  5. Notification. Category = @ "alert";
  6. [[UIApplication sharedapplication] schedulelocalnotification: notification];
  7. //Use these two methods to determine if registration is successful   
  8. //NSLog (@ "currentusernotificationsettings =%@", [[UIApplication sharedapplication] Currentusernotificationsettings]);   
  9. //[[uiapplication sharedapplication] isregisteredforremotenotifications];   

6. Dealing with results in APPDELEGATE.M

[OBJC]View PlainCopy
  1. //local push notification   
  2. -(void) application: (uiapplication *) application Didregisterusernotificationsettings:(uiusernotificationsettings *) Notificationsettings
  3. {
  4. //successfully registered Registerusernotificationsettings: After the callback method   
  5. NSLog (@ "%@", notificationsettings);
  6. }
  7. -(void) application: (uiapplication *) application Didreceivelocalnotification:(uilocalnotification *) Notification
  8. {
  9. //method called after local push message is received   
  10. NSLog (@ "%@", notification);
  11. }
  12. -(void) application: (uiapplication *) application Handleactionwithidentifier:(nsstring *) identifier forlocalnotification :( uilocalnotification  *) Notification completionhandler:(void (^) ()) Completionhandler
  13. {
  14. //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   
  15. NSLog (@ "%@----%@", identifier,notification);
  16. Completionhandler (); //Finish processing the message, and finally make sure to call this code block   
  17. }
  18. //Remote push notification   
  19. -(void) application: (uiapplication *) application Didregisterforremotenotificationswithdevicetoken:(nsdata *) Devicetoken
  20. {
  21. //Registered successfully with APNs, received the returned Devicetoken   
  22. }
  23. -(void) application: (uiapplication *) application Didfailtoregisterforremotenotificationswitherror:(nserror *) Error
  24. {
  25. //Register failed to APNs, return error message   
  26. }
  27. -(void) application: (uiapplication *) application Didreceiveremotenotification:(nsdictionary *) UserInfo
  28. {
  29. //Receive remote push notification messages   
  30. }
  31. -(void) application: (uiapplication *) application Handleactionwithidentifier:(nsstring *) identifier forremotenotification :( nsdictionary  *) userInfo completionhandler:(void (^) ()) Completionhandler
  32. {
  33. //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   
  34. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.