ios 添加本地推送UNUserNotifications

1.首先需要导入UserNotifications.framework框架;

2.在Appdelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中注册本地推送:[self registerAPN];

//注册本地推送

- (void)registerAPN {

//    if (@available(iOS 10.0, *)) { // iOS10 以上

        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        center.delegate=self;

        UNAuthorizationOptions types=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;

         [centerrequestAuthorizationWithOptions:typescompletionHandler:^(BOOLgranted,NSError*_Nullableerror) {

          }];

}

3.代码实现本地推送

-(void)addLocationNotifitionWithFrencyWith:(NSString *)text

{

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    content.title=@"本地推送";

    content.subtitle= text;

    content.sound= [UNNotificationSound   defaultSound];

    content.userInfo=@{@"type":@"333"};

    content.badge=@1;

    //多少时间之后发送本地推送

    NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:0.5] timeIntervalSinceNow];

    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];


    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"iiid" content:content trigger:trigger];

        //把通知加到UNUserNotificationCenter, 到指定触发点会被触发

    [centeraddNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

            if(error) {

                NSLog(@"通知添加失败:%@",error);

            }else{

                NSLog(@"通知添加成功");

            }

    }];

}

4.在UNUserNotificationCenter的delegate方法中添加点击推送之后的操作

#warning 前台收到推送

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

{

    NSLog(@"点开推送啦啦啦");

}

#warning 后台点击推送

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

{

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;//消掉小红点

    NSDictionary *userInfo = response.notification.request.content.userInfo;

    NSLog(@"收到推送\n详细内容:%@",userInfo);

}

5.移除本地推送

(1)  //移除所有的推送通知

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    [centerremoveAllPendingNotificationRequests];

(2)移除某一个推送:根据UNNotificationRequest的identifier 找到特定的推送请求,再移除

-(void)removeOneNotificationWithID:(NSString*)noticeId

{

UNUserNotificationCenter*center=[UNUserNotificationCenter currentNotificationCenter];

[center getPendingNotificationRequestsWithCompletionHandler: ^(NSArray<UNNotificationRequest*>*_Nonnull requests)

{

for(UNNotificationRequest*req   in   requests)

{

NSLog(@"The exist IDs:%@\n",req.identifier);

}

NSLog(@"Remove currentID:%@",noticeId);

}];

[center removePendingNotificationRequestsWithIdentifiers:@[noticeId]];

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。