iOS10 a detailed development tutorial for the latest implementation of remote notifications
- 2020-11-30 08:35:26
- OfStack
1. Introduction to iOS push Notifications
It's well known that Apple's push notifications start with iOS3 and are updated every year. For example, Silent remote notifications(remote silent push) appears on iOS7, Category(category, also known as quick reply) appears on iOS8, and Text Input action(text box quick reply) appears on iOS9.
With iOS10, Apple took the big step of updating both remote and local notifications. iOS10 introduced a new UserNotifications framework (iOS10 was previously part of the UIKit framework).
The new push notification framework integrates the click processing methods of local push and remote push, making the previously dedicated push notification method only handle silent push.
2. Introduction of remote Push notification
1. What is remote push
In the case of networking, the remote server pushes notifications to the client, also known as APNs(Apple Push Notification Services). No matter whether the application is opened or closed, the remote notifications pushed by the server can be received. In the case of networking, all Apple devices will establish a long connection with Apple server
2. Implementation principle of remote push:
1. When opening App: Send
UDID
and
BundleID
to
APNs
Return after encryption
deviceToken
2. To obtain
Token
After App calls the interface, the user identity information and
deviceToken
Send to the server, the server records
3. When the push message is sent, the server finds the stored information according to the user's identity information
deviceToken
, combine the message and
deviToken
Sent to the
APNs
4. Apple's APNs is approved
deviceToken
, find the specified program of the specified device, and push the message to the user
3. Premise for remote push function
1. The real machine
2. Certificate during debugging
BundleID
0
A certificate for debugging a real machine
aps_development.cer
Certificate used for push debugging of real machine
xxx.mobileprovision
Description file that records the mobile phone, computer and program that can be debugged
3. Certificates at the release stage
iOS_distribution.cer
The certificate used to issue app
aps.cer
Certificate to enable app to have push functionality at release time
xxx.mobileprovision
Description file that records the computer that was able to publish the app
How to configure certificates that are not included in this tutorial, leave it to the reader, or refer to the video tutorial
3. iOS10 new Remote Notification tutorial
1. Register remote push and get DeviceToken
1. Create a project for iOS and enter the project name
In 2.
AppDelegate
Import header file:
#import <UserNotifications/UserNotifications.h>
In 3.
application:didFinishLaunchingWithOptions
Method to register remote notifications
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Request notification authority , Local and remote sharing
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@" The request is successful ");
} else {
NSLog(@" The request failed ");
}
}];
// Register for remote notification
[[UIApplication sharedApplication] registerForRemoteNotifications];
// Set the proxy for notification
center.delegate = self;
return YES;
}
4. When receiving remote push
DeviceToken
Method, get Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// This will be needed in the future Token Upload to the backend server
NSLog(@"token:%@", deviceToken);
}
2. Processing method of iOS10 remote Push notification
When you click push, if you want to process it, in iOS10, you also need to set it
UNUserNotificationCenter
the
delegate
And shall comply with the
UNUserNotificationCenterDelegate
The agreement.
And implement the following three methods for handling the different cases of click notification
willPresentNotification:withCompletionHandler
For foreground operation
didReceiveNotificationResponse:withCompletionHandler
Used for background and program exit
didReceiveRemoteNotification:fetchCompletionHandler
For silent push
// Set the proxy for notification
center.delegate = self;
1. The method will be called by the foreground operation
Foreground run: Refers to the program is running, the user can see the interface of the program.
Notification banners appear in iOS10, whereas in previous frameworks, notification banners do not appear when the foreground is running.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary *userInfo = notification.request.content.userInfo;
// Foreground operation push red Label
[self showLabelWithUserInfo:userInfo color:[UIColor redColor]];
// Can be set when notified , What effects are present ( voice / remind / Digital Angle standard )
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
2. Methods that will be called after background operation and program exit
Background run: Refers to the program is open, the user can not see the interface of the program, such as lock screen and press Home key.
Program exit: When the program is not running, or when the program is closed by double-clicking the Home key.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
NSDictionary *userInfo = response.notification.request.content.userInfo;
// Background and exit push According to the green Label
[self showLabelWithUserInfo:userInfo color:[UIColor greenColor]];
completionHandler();
}
3. Methods called by silent push notifications
Silent push: when iOS7 appears after that, there will be no reminder or sound.
Requirements:
Push the
payload
Cannot contain
alert
and
sound
field
You need to add
content-available
Field and set the value to 1
Such as:
{"aps":{"content-available":"1"},"PageKey”":"2"}
// If it's an old frame , This method The front desk / The background / exit / Silent push can be handled
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// Silent push According to the blue Label
[self showLabelWithUserInfo:userInfo color:[UIColor blueColor]];
completionHandler(UIBackgroundFetchResultNewData);
}
4. Common methods for handling notifications
In development, the logic of clicking a notification should depend on the requirements of your application.
Here, for the sake of demonstration, simply pass the value of the notification
deviceToken
0
Displays on the main screen.
- (void)showLabelWithUserInfo:(NSDictionary *)userInfo color:(UIColor *)color
{
UILabel *label = [UILabel new];
label.backgroundColor = color;
label.frame = CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 300);
label.text = userInfo.description;
label.numberOfLines = 0;
[[UIApplication sharedApplication].keyWindow addSubview:label];
}
3. Test remote push
deviceToken
1
Mac is a simple Mac applet that simulates servers and can submit content to Apple's APNs server.
To test remote notifications, we need to install this program.
Go to www.github.com, search and download
deviceToken
1
When using:
Compile the project and, if error is reported, comment the code that reported the error without affecting actual use.
Enter the Apple developer website, obtain the remote push certificate used for debugging the real machine, and import it into the project
I'm going to take what I got earlier
DeviceToken
, and the test text, fill in the item
AppDelegate
In the
init
Methods.
When this project is run, an Mac applet will appear. Click Push to send remote notification.
- (id)init {
self = [super init];
if(self != nil) {
self.deviceToken = @"de20184c ef0461d5 12c76422 f5b78240 5f657e18 ebf91c9f 01d5560c e2913102";
self.payload = @"{\\"aps\\":{\\"alert\\":{\\"title\\":\\"himeao\\",\\"subtitle\\":\\" self-taught \\",\\"body\\":\\"iOS10 The remote & Local push tutorial \\"},\\"badge\\":1,\\"sound\\":\\"default\\"},\\"PageKey\\":\\"1\\"}";
self.certificate = [[NSBundle mainBundle] pathForResource:@"aps_development" ofType:@"cer"];
}
return self;
}
conclusion
Above is the latest iOS10 remote notification development tutorial content, the content of this article for you to learn iOS10 is still very reference value, hope to be helpful to you iOS developers, if you have any questions can leave a message to communicate.