日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

iOS开发本地推送(iOS10)UNUserNotificationCenter

發布時間:2023/12/13 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 iOS开发本地推送(iOS10)UNUserNotificationCenter 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、簡介

  iOS10之后蘋果對推送進行了封裝,UNUserNotificationCenter就這樣產生了。簡單介紹本地推送的使用UserNotifications官方文檔說明!

2、簡單使用UNUserNotificationCenter

  一、創建UNUserNotificationCenter,設置推送模式和代理!

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

  二、設置推送內容

        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.title = @"推送中心標題";
        content.subtitle = @"副標題";
        content.body  = @"這是UNUserNotificationCenter信息中心";
        content.badge = @20;
        content.categoryIdentifier = @"categoryIdentifier";
        
//        需要解鎖顯示,紅色文字。點擊不會進app。
//        UNNotificationActionOptionAuthenticationRequired = (1 << 0),
//
//        黑色文字。點擊不會進app。
//        UNNotificationActionOptionDestructive = (1 << 1),
//
//        黑色文字。點擊會進app。
//        UNNotificationActionOptionForeground = (1 << 2),
        
        UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"enterApp"
                                                                            title:@"進入應用"
                                                                          options:UNNotificationActionOptionForeground];
        UNNotificationAction *clearAction = [UNNotificationAction actionWithIdentifier:@"destructive"
                                                                                 title:@"忽略2"
                                                                               options:UNNotificationActionOptionDestructive];
        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier"
                                                                                  actions:@[action,clearAction]
                                                                        intentIdentifiers:@[requestID]
                                                                                  options:UNNotificationCategoryOptionNone];
        [center setNotificationCategories:[NSSet setWithObject:category]];

  三、設置推送方式

        UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestID content:content trigger:timeTrigger];

  trigger的其它用法:

        //1分鐘后提醒
        UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];
        
        //每小時重復 1 次
        UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 repeats:YES];
        
        //周日早8點
        NSDateComponents *components = [[NSDateComponents alloc] init];
        components.weekday = 1;
        components.hour = 8;
        UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
        
        //#import <CoreLocation/CoreLocation.h>
        CLRegion *region = [[CLRegion alloc] init];
        UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];

  四、添加推送request

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

3、UNUserNotificationCenter的Delegate

//將要推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSLog(@"----------willPresentNotification");
}
//已經完成推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    NSLog(@"============didReceiveNotificationResponse");
    NSString *categoryID = response.notification.request.content.categoryIdentifier;
    if ([categoryID isEqualToString:@"categoryIdentifier"]) {
        if ([response.actionIdentifier isEqualToString:@"enterApp"]) {
            if (@available(iOS 10.0, *)) {
                
            } else {
                // Fallback on earlier versions
            }
        }else{
            NSLog(@"No======");
        }
    }
    completionHandler();
}

4、移除推送

        [center removePendingNotificationRequestsWithIdentifiers:@[requestID]];
        [center removeAllDeliveredNotifications];

附錄:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if (@available(iOS 10.0, *)) {
        //第一步:獲取推送通知中心
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (!error) {
                                      NSLog(@"succeeded!");
                                  }
                              }];
        center.delegate = self;
        
        //第二步:設置推送內容
        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.title = @"推送中心標題";
        content.subtitle = @"副標題";
        content.body  = @"這是UNUserNotificationCenter信息中心";
        content.badge = @20;
        content.categoryIdentifier = @"categoryIdentifier";
        
        
//        需要解鎖顯示,紅色文字。點擊不會進app。
//        UNNotificationActionOptionAuthenticationRequired = (1 << 0),
//
//        黑色文字。點擊不會進app。
//        UNNotificationActionOptionDestructive = (1 << 1),
//
//        黑色文字。點擊會進app。
//        UNNotificationActionOptionForeground = (1 << 2),
        

        UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"enterApp"
                                                                            title:@"進入應用"
                                                                          options:UNNotificationActionOptionForeground];
        UNNotificationAction *clearAction = [UNNotificationAction actionWithIdentifier:@"destructive"
                                                                                 title:@"忽略2"
                                                                               options:UNNotificationActionOptionDestructive];
        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier"
                                                                                  actions:@[action,clearAction]
                                                                        intentIdentifiers:@[requestID]
                                                                                  options:UNNotificationCategoryOptionNone];
        [center setNotificationCategories:[NSSet setWithObject:category]];

        //第三步:設置推送方式
        UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestID content:content trigger:timeTrigger];
        
        //第四步:添加推送request
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            
        }];
        
        
        [center removePendingNotificationRequestsWithIdentifiers:@[requestID]];
        [center removeAllDeliveredNotifications];
//        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
//            NSLog(@"settings===%@",settings);
//        }];
    } else {
    }
    return YES;
}

#pragma mark - UNUserNotificationCenterDelegate
//將要推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSLog(@"----------willPresentNotification");
}
//已經完成推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    NSLog(@"============didReceiveNotificationResponse");
    NSString *categoryID = response.notification.request.content.categoryIdentifier;
    if ([categoryID isEqualToString:@"categoryIdentifier"]) {
        if ([response.actionIdentifier isEqualToString:@"enterApp"]) {
            if (@available(iOS 10.0, *)) {
                
            } else {
                // Fallback on earlier versions
            }
        }else{
            NSLog(@"No======");
        }
    }
    completionHandler();
}

總結

以上是生活随笔為你收集整理的iOS开发本地推送(iOS10)UNUserNotificationCenter的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。