iOS APNS

APNS推送机制

APNS推送机制
iOS APNS_第1张图片
APNS详细工作流程

APNS注意事项

1、APNS免费,但需要开发者账号
2、APNS不稳定,Apple对消息推送的可靠性不做保证
3、APNS消息大小有限制,iOS8以下256字节,iOS8以上2KB,基于HTTP/2的全新APNS协议4KB

调试准备

1、开发者账号

2、PushMeBaby GitHub:https://github.com/stefanhafeneger/PushMeBaby

pushMeBaby小坑总结:
2.1、Xib版本太早,修改Build版本
2.2、ioSock.h中引入头文件报错,修改为#include
2.3、导入生成的cer
2.4、deviceToken不带尖括号
2.5、注意网络问题

iOS API

1、请求权限

iOS8-10,用户点击允许后才能收到推送

// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenter requestAuthorizationWithOptions:completionHandler:] and -[UNUserNotificationCenter setNotificationCategories:]") __TVOS_PROHIBITED;

iOS10之后可使用UserNotification.framework

// User authorization is required for applications to notify the user using UNUserNotificationCenter via both local and remote notifications.
- (void)requestAuthorizationWithOptions:(UNAuthorizationOptions)options completionHandler:(void (^)(BOOL granted, NSError *__nullable error))completionHandler;

eg.

[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {}];

2、注册远程推送

iOS8之前,iOS8后使用报Waring:registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types NS_DEPRECATED_IOS(3_0, 8_0, "Use -[UIApplication registerForRemoteNotifications] and UserNotifications Framework's -[UNUserNotificationCenter requestAuthorizationWithOptions:completionHandler:]") __TVOS_PROHIBITED;

iOS8之后:

// Calling this will result in either application:didRegisterForRemoteNotificationsWithDeviceToken: or application:didFailToRegisterForRemoteNotificationsWithError: to be called on the application delegate. Note: these callbacks will be made only if the application has successfully registered for user notifications with registerUserNotificationSettings:, or if it is enabled for Background App Refresh.
- (void)registerForRemoteNotifications NS_AVAILABLE_IOS(8_0);

3、注册回执

注册成功,获取deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);

注册失败,得到error

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);

4、收到推送消息

iOS3-10

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications");

iOS7之后:

/*! This delegate method offers an opportunity for applications with the "remote-notification" background mode to fetch appropriate new data in response to an incoming remote notification. You should call the fetchCompletionHandler as soon as you're finished performing that operation, so the system can accurately estimate its power and data cost.
 
 This method will be invoked even if the application was launched or resumed because of the remote notification. The respective delegate methods will be invoked first. Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented. !*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);

iOS10,应用在前台收到推送消息,控制显示与否

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14);

iOS10,响应推送通知

// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14) __TVOS_PROHIBITED;

收到推送的时机

1、App未在后台,点击推送通知唤起App,走didFinishLaunchingWithOptions:方法。launchOptions中有Key为UIApplicationLaunchOptionsRemoteNotificationKey的推送消息内容

iOS APNS_第2张图片
App被push唤起

2、App在后台睡眠,点击推送通知唤醒App,走didReceiveRemoteNotification:方法(iOS7之前,具体看实现的哪个方法)

iOS APNS_第3张图片
App被push唤醒

3、App在前台,iOS10之前不显示通知,直接走didReceiveRemoteNotification:方法

iOS APNS_第4张图片
App在前台

iOS10之后可控制显示与否并得到点击回执,用户点击后,走didReceiveNotificationResponse:方法

iOS APNS_第5张图片
App在前台,点击通知

你可能感兴趣的:(iOS APNS)