應(yīng)用背景
1.通知:一般app收到一個通知消息,打開之后鏈接跳轉(zhuǎn)到一個指定的網(wǎng)頁锐朴。
- deepLink: 前端頁面點擊某個button傳來一個deepLink,觸發(fā)之后跳到指定頁面蔼囊。(如banner廣告的點擊事件)包颁。
使用APNs 步驟
1. 向用戶申請通知權(quán)限
iOS 8 - iOS 10
open func registerUserNotificationSettings(_ notificationSettings: UIUserNotificationSettings)
iOS 10以后
open func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping (Bool, Error?) -> Void)
2. 注冊通知
UIApplication.shared.registerForRemoteNotifications()
3. 注冊完通知會走appDelegate的代理方法返回一個發(fā)送通知的唯一標(biāo)識符。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
{
//把得到的newDeviceToken發(fā)送給后臺服務(wù)器(例如我們用第三方的服務(wù)mixpanel)
[AnalyticsManager setPushDeviceToken:newDeviceToken];
}
4. 后臺篩出要發(fā)送通知的人压真,將其newDeviceToken發(fā)送給APNs
5. APNs再發(fā)送給app娩嚼。就可以看到類似這樣的東西了
6. 點擊通知后的回調(diào),會返回一個response滴肿,response包含通知數(shù)據(jù)岳悟,取出數(shù)據(jù)進行解析,做相應(yīng)的操作即可泼差。
- (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;
remoteNotificationDict = [NSDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
7. 如果暫時不想要接收到通知可以unregisterForRemoteNotifications
贵少。(例如用戶退出登錄之后不想給未登錄用戶發(fā)送登錄的通知)
Discussion
You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app. Apps unregistered through this method can always re-register.
UIApplication.shared.unregisterForRemoteNotifications()
使用deepLink
1. 檢測scheme是否可以被處理,客戶端要想能打開這段URL堆缘,需要配置相應(yīng)的URL Schemes滔灶。
[[UIApplication sharedApplication] canOpenURL:url]
2. 通過url開啟應(yīng)用
[[UIApplication sharedApplication] openURL:url]
3. 處理openURL邏輯
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options NS_AVAILABLE_IOS(9_0);
deepLink 和 推送聯(lián)系
推送可以附帶一段數(shù)據(jù),可以將數(shù)據(jù)中附帶一段URL吼肥,然后就和deepLink一樣了录平,只需要寫一個方法處理URL即可麻车。原理都是處理URL,解析URL斗这,然后做相應(yīng)的處理动猬。