Play iOS 10 push – UserNotifications Framework (last)

IOS 10, came

In June 14th this year after the apple Developers Conference WWDC 2016, I quickly went to Apple developers to download the latest Xcode 8 beta and iOS 10 beta, and then on his mobile phone with iOS 10 beta, ruthlessly experience a.
can say that iOS 10 has made a lot of changes, both from the interface style and to the Framework. The most intuitive feeling is that the corners of the interface have increased, the system animation is more diverse and smooth, and the functions of the system App have become richer.

And the push feature in iOS 10 is stronger than before,
, today we’re going to talk about the push feature in iOS 10.

Notifications before iOS 10

First, let’s briefly review the previous push services for iOS 10.
iOS push is divided into Local Notifications (local push) and Remote Notifications (remote push), first look at the 2 chart:

Play iOS 10 push - UserNotifications Framework (last)
Local Notifications
Play iOS 10 push - UserNotifications Framework (last)
Remote Notifications

Simply put, the local push is customized locally through the App, added to the Schedule of the system, and then pushed the specified text at the specified time. The remote push through the server to the apple push server Apple Push Notification Service (APNs) to send Notification Payload, and then push the APNs sent to the designated device designated App. After
and iOS 7, the silent push is carried out without disturbing the user explicitly:

Play iOS 10 push - UserNotifications Framework (last)
Silent Push

Specific practices can refer to iOS 7, Background, Remote, Notification

User Notifications Framework


Well, that’s all, it’s time to get to the point today – User Notifications Framework.
first in AppDelegate.m

Import
#import < UserNotifications/UserNotifications.h>
Registration push

The following are the registration methods before and after iOS 10, where the value of the 1 UNAuthorizationOptionCarPlay can also be found for the UNAuthorizationOptions system.

- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary *) launchOptions //iOS 10 before UIUserNotificationSettings {*settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert UIUserNotificationTypeBadge | | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; //iOS UNUserNotificationCenter *center = 10 [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions: (UNAuthorizationOptionBadge | | UNAuthorizationOptionSound UNAuthorizationOptionAlert completionHandler:^ (BOOL) granted, NSError * _Nullable (error) {if! Error) {NSLog (request @ authorizatio N succeeded! ");}}]; return YES;}
Notification settings

Before registering push services, IOS 8 and before used different API, and returned results differently. Now, apple not only unifies the API, but we can get more detailed settings for the user.

[center getNotificationSettingsWithCompletionHandler:^ (UNNotificationSettings * _Nonnull settings) {NSLog ("% @ @", settings);}];

Print to get the following information:

< UNNotificationSettings: 0x16567310; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle: Banner>
Token Registration

The same as before

[[UIApplication sharedApplication] registerForRemoteNotifications];
Content

Previously only one text was displayed, and now you can have title, subtitle, and body.

Play iOS 10 push - UserNotifications Framework (last)

The
customization method is as follows:

//Local Notification UNMutableNotificationContent *content [[UNMutableNotificationContent alloc] = init]; content.title = @ "Introduction to Notifications"; content.subtitle = @ "Session 707"; content.body = @ "Woah! These new notifications look amazing Don t you agree! '?"; content.badge = @1; //Remote Notification {"APS": {{"alert": "Title": "Introduction to Notifications", "subtitle", "Session 707", "body": "Woah! These new notifications look amazing Don t you agree!'?}", "badge": 1}},
Triggers

Another new feature, there are three

  • UNTimeIntervalNotificationTrigger
  • UNCalendarNotificationTrigger
  • UNLocationNotificationTrigger
After //2 minutes UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 reminded repeats:NO]; / / per hour repeated 1 times I call UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 water repeats:YES]; / / 8:00 every Monday morning to remind me to do breakfast NSDateComponents *components [[NSDateComponents wife alloc] = init]; components.weekday = 2; components.hour = 8; UNCalendarNotificationTrigger = *trigger3 [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; //#import < CoreLocation/CoreLocation.h> a call to McDonald's; / / *region = [[CLRegion alloc] CLRegion me off init]; UNLocationNotificationTrigg Er *trigger4 = [UNLocationNotificationTrigger, triggerWithRegion:region, repeats:NO];
Add Request
NSString @ *requestIdentifier = "sampleRequest"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:trigger1]; [center addNotificationRequest:request withCompletionHandler:^ (NSError * _Nullable error) {}];

Push summary

Then the whole push becomes Jiangzi:

Play iOS 10 push - UserNotifications Framework (last)
  • Local Notifications implements this trilogy by defining Content and Trigger for request to UNUserNotificationCenter.
  • Remote Notifications sends Notification Payload to APNs.
Notification Handling

Set the push, and then it’s over IOS 10 isn’t that simple!
captures and processes the upcoming tweets when the App is in the foreground by implementing the protocol:

@interface AppDelegate (<); UNUserNotificationCenterDelegate> - (void) userNotificationCenter: (UNUserNotificationCenter * Center) willPresentNotification: (UNNotification * notification) (withCompletionHandler: (void ^) (UNNotificationPresentationOptions)) completionHandler{completionHandler (UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);}

Let it show only alert and sound, and ignore badge.

Notification Management

Take full control of the push cycle:

  • Local Notification by updating request
  • Remote Notification through new field apns-collapse-id

Through the previous addNotificationRequest: method, in the case of ID unchanged, re – add, you can refresh the original push.

NSString @ *requestIdentifier = "sampleRequest"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:newContent trigger:newTrigger1]; [center addNotificationRequest:request withCompletionHandler:^ (NSError * _Nullable error) {}];

Delete planned push:

[center removePendingNotificationRequestsWithIdentifiers:@[requestIdentifier]];

In addition, there are powerful interfaces such as deleting all tweets, checking the tweets that have been sent, deleting the tweets that have been sent, and so on. UNUserNotificationCenter.h.

After updating the original push, there will be corresponding changes in the display of the notification center. Here, note second messages, and the score is 1:0
now

Play iOS 10 push - UserNotifications Framework (last)

is 1:1 after the score is refreshed and the position is pre positioned without producing a new push!

Play iOS 10 push - UserNotifications Framework (last)

This method consider using
, refresh push continuously, can not make the push content shows the top of the notification center of mobile phone users in their App, beat the rest of everything? Always feel a bit unkind ah ~!

Advanced Notifications


For more advanced features like push Media Attachments, we’ll discuss it in detail in the next article.

Media Attachments

Add more multimedia attachments for push, such as pictures, music,

Play iOS 10 push - UserNotifications Framework (last)

Go on to the next fun iOS 10 push (medium)