日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > [置頂] iOS10--消息通知的基本使用

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

來(lái)源:程序員人生   發(fā)布時(shí)間:2016-11-14 09:50:55 閱讀次數(shù):4485次

官方將通知單獨(dú)放在了UserNotifications.framework,使用時(shí)需要導(dǎo)入框架。
UserNotifications.framework主要類(lèi)文件:

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

UNUserNotificationCenter的利用:

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

    // 創(chuàng)建1個(gè)本地通知 UNMutableNotificationContent *content_1 = [[UNMutableNotificationContent alloc] init]; // 主標(biāo)題 content_1.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil]; // 副標(biāo)題 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]; // 設(shè)置觸發(fā)時(shí)間 UNTimeIntervalNotificationTrigger *trigger_1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; // 創(chuàng)建1個(gè)發(fā)送要求 UNNotificationRequest *request_1 = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content_1 trigger:trigger_1];

    補(bǔ)充:

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

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

接收通知
  • 處理通知:
    設(shè)置UNUserNotificationCenterDelegate:
    注意:UNUserNotificationCenter 的 delegate 必須在 application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: 方法中實(shí)現(xiàn);

    center.delegate = self;
    • 利用內(nèi)展現(xiàn)通知:

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

UNNotificationCategory的利用:

  • 創(chuàng)建1個(gè) category:
    /* UNNotificationActionOptionAuthenticationRequired = (1 << 0), UNNotificationActionOptionDestructive = (1 << 1), 取消 UNNotificationActionOptionForeground = (1 << 2), 啟動(dòng)程序 */ 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];
  • 在創(chuàng)建 UNNotificationContent 時(shí)把 categoryIdentifier 設(shè)置為需要的 category id 便可:
    content.categoryIdentifier = @"my_category";

    遠(yuǎn)程推送也能夠使用 category,只需要在 payload 中添加 category 字段,并指定預(yù)先定義的 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]; } } } } }]; }

長(zhǎng)按 3D touch 效果圖

進(jìn)入利用
iOS 10 中被標(biāo)為棄用的 API

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



文/芝麻綠豆(簡(jiǎn)書(shū)作者)
原文鏈接:http://www.jianshu.com/p/98c740600dfa
著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者取得授權(quán),并標(biāo)注“簡(jiǎn)書(shū)作者”。

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線(xiàn)----------------------------
分享到:
------分隔線(xiàn)----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产精品高清一区 | 日韩精品久久久久 | 99在线精品免费视频九九视 | 99精品视频在线 | 日韩精品一区二区三区中文字幕 | 成人欧美一区二区三区黑人 | 成人在线播放 | 国产精品日韩在线观看 | 国产一级黄色毛片 | 视频一区二区三区在线观看 | 在线观看av免费 | 成人精品国产免费网站 | 一区二区三区中文 | 日韩在线一区二区三区 | 久久久久久中文 | 在线激情av| 黄色一级片在线看 | 久久se精品一区精品二区 | 久久影视精品 | av片免费| 黄色一级大片在线观看 | 草久久| 91成人国产 | 一区二区三区在线 | 欧美日本韩国一区二区三区 | 欧美成在线观看 | 一区二区视频网站 | 久久久国产精品一区二区三区 | 麻豆视频观看 | 日韩电影免费 | 一区二区亚洲 | 美女国内精品自产拍在线播放 | 日本中文字幕免费 | 一区二区三区久久 | 亚洲影院一区 | 黄色av网站在线免费观看 | 精品自拍视频 | 国产精品igao视频网网址不卡日韩 | 国产成人精品三级麻豆 | 在线免费观看亚洲 | 欧美专区视频 |