IOS8 has a new notification center and a new notification mechanism. When the push is received at the top of the screen, you only need to pull down to see the quick operation interface, and you do not need to enter the application to operate. On the screen lock page, you can quickly process push projects. Basically, you are allowed to process push information without leaving the current page, improving processing efficiency again.
Text messages, emails, calendars, reminders, and third-party applications that can directly interact with each other allow you to perform quick operations without entering the program and focus on what you are doing.
- Quickly reply to information in the notification banners without entering the text message program;
- You can directly reject or accept the email invitation;
- Reminders can be marked as completed or postponed;
- After the third-party application update interface, you can directly perform quick operations on the application.
I recently studied the official documentation of ios8. I have studied this function and the results have basically come out. Because remote message pushing is complicated and requires background support, I use local push instead. Basically, the proxy methods to be called are similar. Let's talk about the basic process below:
1. Create the action to be added to the message (displayed as a button)
UIMutableUserNotificationAction * action = [[UIMutableUserNotificationAction alloc] init]; action. identifier = @ "action"; // The identifier of the button action. title = @ "Accept"; // The title action of the button. activationMode = UIUserNotificationActivationModeForeground; // start the program when you click it. // action. authenticationRequired = YES; // action. destructive = YES; UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init]; // the second button action2.identifier = @ "action2"; action2.title = @ "Reject"; action2.activationMode = success; // when clicked, the program is not started and the action is processed in the background. authenticationRequired = YES; // the token must be unlocked before processing. activationMode = UIUserNotificationActivationModeForeground; this attribute is ignored; action. destructive = YES;
2. Create a category set of actions (buttons)
UIMutableUserNotificationCategory * categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys. identifier = @ "alert"; // the unique identifier of this group of actions [categorys setActions: @ [action, action2] forContext :( UIUserNotificationActionContextMinimal)];
3. Create UIUserNotificationSettings and set the display class type of the message.
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
4. Register for push
[[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
The offline push data packet carries a specific Category field (the Field content must be defined in the front and back, and must be consistent). When the mobile phone receives the field, the buttons corresponding to the Category setting of the above Code can be displayed, and response button events.
// Payload example: {"aps": {"alert": "Incoming call", "sound": "default", "badge": 1, "category ": "incomingCall "}}
Major modification: Before an offline push data packet, the data can contain a maximum of 256 bytes. Now APPLE scales this value to 2 kb. This should be for iOS 8 only.
5. Initiate a local push message
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 whether the registration is successful. // NSLog (@ "currentUserNotificationSettings = % @", [[UIApplication sharedApplication] currentUserNotificationSettings]); // [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
6. process the results in AppDelegate. m.
// Push notifications locally-(void) application :( UIApplication *) application didRegisterUserNotificationSettings :( UIUserNotificationSettings *) icationicationsettings {// After registerUserNotificationSettings is successfully registered, callback method NSLog (@ "% @", notificationSettings);}-(void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {// method NSLog (@ "% @", notification);}-(void) application :( UIApplication *) application handleActionWithIdentifier :( NSString *) called after receiving the local push message *) identifier forLocalNotification :( UILocalNotification *) notification completionHandler :( void (^) () completionHandler {// receives a local message when it is not in the App interface. The drop-down message will have a shortcut to reply, click the button and call the method. The identifier determines which button is clicked. notification indicates the message content NSLog (@ "% @ ---- % @", identifier, notification); completionHandler (); // after processing the message, make sure to call this code block} // remote push notification-(void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {// register with APNS successfully, and receive the returned deviceToken}-(void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {// failed to register with APNS, returned error message error}-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo {// receive the remote push notification message}-(void) application :( UIApplication *) application handleActionWithIdentifier :( NSString *) identifier forRemoteNotification :( NSDictionary *) userInfo completionHandler :( void (^) () completionHandler {// The server receives the PUSH message when the App is not started, the drop-down message will have a quick reply button. click the button and call the method to determine which button to click Based on identifier}
After running, you need to press shift + command + H to push the program to the background, or press command + L to lock the simulator screen to see the effect!
If the program is returned to the background, and the pull-down message is received, the two buttons added just now will appear. If the screen is locked, after the message appears, the two buttons just added will appear on the left.
The effect is as follows:
Now we can only display the button in the message. The input box is still under study. If you have studied it, thank you for sharing it. Let's make it better together!
Code: https://github.com/ios44first/PushDemo
(If You Want To reprint please indicate the address, thank you http://blog.csdn.net/yujianxiang666/article/details/35260135)