多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > [置頂] iOS10--消息通知的基本使用

[置頂] iOS10--消息通知的基本使用

來源:程序員人生   發布時間:2016-11-14 09:50:55 閱讀次數:4456次

官方將通知單獨放在了UserNotifications.framework,使用時需要導入框架。
UserNotifications.framework主要類文件:

UNCalendarNotificationTrigger
UNLocationNotificationTrigger
UNMutableNotificationContent
UNNotification
UNNotificationAction
UNNotificationAttachment
UNNotificationCategory
UNNotificationContent
UNNotificationRequest
UNNotificationResponse
UNNotificationServiceExtension
UNNotificationSettings
UNNotificationSound
UNNotificationTrigger
UNPushNotificationTrigger
UNTextInputNotificationAction
UNTextInputNotificationResponse
UNTimeIntervalNotificationTrigger
UNUserNotificationCenter

UNUserNotificationCenter的利用:

  • 要求用戶授權:
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; // 要求授權 /* UNAuthorizationOptionBadge = (1 << 0), UNAuthorizationOptionSound = (1 << 1), UNAuthorizationOptionAlert = (1 << 2), UNAuthorizationOptionCarPlay = (1 << 3), */ [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
    補充:獲得授權設置信息
    // 獲得通知授權和設置 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { /* UNAuthorizationStatusNotDetermined : 沒有做出選擇 UNAuthorizationStatusDenied : 用戶未授權 UNAuthorizationStatusAuthorized :用戶已授權 */ if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) { NSLog(@"未選擇"); }else if (settings.authorizationStatus == UNAuthorizationStatusDenied){ NSLog(@"未授權"); }else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized){ NSLog(@"已授權"); } }];
  • 創建本地通知:

    // 創建1個本地通知 UNMutableNotificationContent *content_1 = [[UNMutableNotificationContent alloc] init]; // 主標題 content_1.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil]; // 副標題 content_1.subtitle = [NSString localizedUserNotificationStringForKey:@"subtitle" arguments:nil]; content_1.badge = [NSNumber numberWithInteger:1]; content_1.body = [NSString localizedUserNotificationStringForKey:@"title_message_for_yan" arguments:nil]; content_1.sound = [UNNotificationSound defaultSound]; // 設置觸發時間 UNTimeIntervalNotificationTrigger *trigger_1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; // 創建1個發送要求 UNNotificationRequest *request_1 = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content_1 trigger:trigger_1];

    補充:

    • UserNotifications提供了3種觸發器:
      UNTimeIntervalNotificationTrigger :1定時間后觸發 UNCalendarNotificationTrigger : 在某月某日某時觸發 UNLocationNotificationTrigger : 在用戶進入或是離開某個區域時觸發
    • @“my_notification”要求的標識符可以用來管理通知:
      - 移除還未展現的通知 [center removePendingNotificationRequestsWithIdentifiers: @[@“my_notification” ]]; [center removeAllPendingNotificationRequests]; // - (void)cancelAllLocalNotifications; - 移除已展現過的通知 [center removeDeliveredNotificationsWithIdentifiers:@[@“my_notification” ]]; [center removeAllDeliveredNotifications]; - 獲得未展現的通知 [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { NSLog(@"%@",requests); }]; - 獲得展現過的通知 [center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) { NSLog(@"%@",notifications); }];
    • 遠程通知的格式:
      { "aps":{ "alert":{ "title":"I am title", "subtitle":"I am subtitle", "body":"I am body" }, "sound":"default", "badge":1 } }
      具體請參考官方文檔
  • 將通知要求添加到通知中心(UNUserNotificationCenter):

    [center addNotificationRequest:request_1 withCompletionHandler:^(NSError * _Nullable error) { }];

接收通知
  • 處理通知:
    設置UNUserNotificationCenterDelegate:
    注意:UNUserNotificationCenter 的 delegate 必須在 application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: 方法中實現;

    center.delegate = self;
    • 利用內展現通知:

      - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ // 如果不想顯示某個通知,可以直接用空 options 調用 completionHandler: // completionHandler([]) completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound); }
    • 在用戶與你推送的通知進行交互時被調用:
      - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ completionHandler(); NSLog(@"userInfo--%@",response.notification.request.content.userInfo); }

UNNotificationCategory的利用:

  • 創建1個 category:
    /* UNNotificationActionOptionAuthenticationRequired = (1 << 0), UNNotificationActionOptionDestructive = (1 << 1), 取消 UNNotificationActionOptionForeground = (1 << 2), 啟動程序 */ UNTextInputNotificationAction *textAction = [UNTextInputNotificationAction actionWithIdentifier:@"my_text" title:@"text_action" options:UNNotificationActionOptionForeground textInputButtonTitle:@"輸入" textInputPlaceholder:@"默許文字"]; UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"my_action" title:@"action" options:UNNotificationActionOptionDestructive]; UNNotificationAction *action_1 = [UNNotificationAction actionWithIdentifier:@"my_action_1" title:@"action_1" options:UNNotificationActionOptionAuthenticationRequired]; /* UNNotificationCategoryOptionNone = (0), UNNotificationCategoryOptionCustomDismissAction = (1 << 0), UNNotificationCategoryOptionAllowInCarPlay = (2 << 0), */ UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"my_category" actions:@[textAction,action,action_1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; NSSet *setting = [NSSet setWithObjects:category, nil]; [center setNotificationCategories:setting];
  • 在創建 UNNotificationContent 時把 categoryIdentifier 設置為需要的 category id 便可:
    content.categoryIdentifier = @"my_category";

    遠程推送也能夠使用 category,只需要在 payload 中添加 category 字段,并指定預先定義的 category id 就能夠了

  • 處理category的通知:
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ completionHandler(); NSLog(@"userInfo--%@",response.notification.request.content.userInfo); // 獲得通知中心的所有的Categories [center getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> * _Nonnull categories) { for (UNNotificationCategory *category in categories) { if ([category.identifier isEqualToString:@"my_category"] && [response.notification.request.content.categoryIdentifier isEqualToString:@"my_category"]) { for (UNNotificationAction *textAction in category.actions) { if ([textAction.identifier isEqualToString:@"my_text"]) { UNTextInputNotificationAction *text = (UNTextInputNotificationAction *)textAction; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.textInputButtonTitle preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleCancel handler:nil]]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil]; } } } } }]; }

長按 3D touch 效果圖

進入利用
iOS 10 中被標為棄用的 API

UILocalNotification
UIMutableUserNotificationAction
UIMutableUserNotificationCategory
UIUserNotificationAction
UIUserNotificationCategory
UIUserNotificationSettings
handleActionWithIdentifier:forLocalNotification:
handleActionWithIdentifier:forRemoteNotification:
didReceiveLocalNotification:withCompletion:
didReceiveRemoteNotification:withCompletion:



文/芝麻綠豆(簡書作者)
原文鏈接:http://www.jianshu.com/p/98c740600dfa
著作權歸作者所有,轉載請聯系作者取得授權,并標注“簡書作者”。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 免费看h的网站 | 国产日比视频 | 欧美另类精品一区二区三区 | 女人天堂网在线观看2019 | 精品女同一区二区三区在线 | 欧美视频一| 日本一区二区三区四区在线观看 | 久久视频精品538在线久 | 女人18一级特级毛片免费看 | 久久综合九九亚洲一区 | jizz亚洲日本 | 97碰碰碰免费公开在线视频 | 久久久精品久久 | 欧美日韩国产高清一区二区三区 | 成人777777| 俺去啦最新地址 | 天天躁夜夜燥2021 | 亚洲国产最新在线一区二区 | 91中文字幕yellow字幕网 | 久久午夜羞羞影院免费观看 | 欧美精品免费一区欧美久久优播 | 国产国产人免费视频成69大陆 | 午夜影院欧美 | 国产精品女上位在线观看 | 欧美日韩亚洲第一页 | www亚洲天堂 | 久久久久在线 | porn日本xxx护士 | 亚洲另类精品xxxx人妖 | 亚洲精品视频在线看 | 久久浮力影院 | 爽爽在线 | 最近中文字幕免费mv视频 | 亚洲人成网站在线播放观看 | 国产成人福利美女观看视频 | 免费麻豆国产一区二区三区四区 | 黄色网址免费看 | 国产美女亚洲精品久久久综合91 | 久久久久久久性 | 亚洲欧洲一区 | 第一色网站 |