iOS本地通知

UserNotifications

Xcode需要打開后臺推送的通知權(quán)限

首先這是一個系統(tǒng)級別的單利獲得

[UNUserNotificationCenter currentNotificationCenter]

請求權(quán)限

得到系統(tǒng)的單利,去請求是否有通知授權(quán)

[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted){
            NSLog(@"通知權(quán)限請求成功");
        }else {
             NSLog(@"通知權(quán)限請求失敗");
        }
    }];

get通知的一些配置信息

- (void)getNotificationSettingsWithCompletionHandler:(void(^)(UNNotificationSettings *settings))completionHandler
  //通知配置等信息

注冊

內(nèi)容

UNMutableNotificationContent->UNNotificationContent->NSObject

設(shè)置對應(yīng)的content信息

  • title->標(biāo)題

  • subtitle->子標(biāo)題

  • body->內(nèi)容

通知類型

  • 本地通知
    • UNTimeIntervalNotificationTrigger 時間觸發(fā)器
    • UNLocationNotificationTrigger 區(qū)域觸發(fā)器
    • UNCalendarNotificationTrigger 日歷觸發(fā)器
  • 遠(yuǎn)程通知

添加通知到對應(yīng)的通知請求里面,每一個請求都有一個Identifier,移除的時候需要根據(jù)identifier去移除.

添加和移除通知

添加通知

- (void)addNotificationRequest:(UNNotificationRequest *)request withCompletionHandler:(nullable void(^)(NSError *__nullable error))completionHandler

添加的時候有一個對應(yīng)的identifier

移除通知

- (void)removePendingNotificationRequestsWithIdentifiers:(NSArray<NSString *> *)identifiers

另外還有兩個代理方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
   //將要請求通知的時候
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
   //收到點(diǎn)擊請求的時候的通知
}

RemoteNotification(iOS8-10)

注冊遠(yuǎn)程通知

[[UIApplication sharedApplication]registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]

把對應(yīng)的deviceToken推送到對應(yīng)的蘋果服務(wù)器,根據(jù)這個deviceToken去識別唯一的設(shè)備標(biāo)識

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

成功或者失敗

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;?- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

  • (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler //處理對應(yīng)的消息
    還有集成第三方的SDK,方便快速集成,但是可能自定義不是很豐富
    騰訊信鴿 阿里云 等等
    推送測試工具

寫在最后

跳轉(zhuǎn)app代碼
這個是在ios8之后加入的,如果是ios8之前參考 app跳轉(zhuǎn)
UIApplication還有一些對應(yīng)的位置,icloud等的一些:

if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                     }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容