首页 > 其他分享 >本地通知UserNotifications的简单使用

本地通知UserNotifications的简单使用

时间:2023-02-03 15:22:49浏览次数:39  
标签:设置 通知 void content NSString UNUserNotificationCenter 本地 UserNotifications

有三个概念要区分下:

(1)通知中心:这个是语法中的设计模式,一对多的广播通知,代码中订阅了该通知的监听者可以接受此通知进行处理

(2)远程通知:也可以说是APNs通知,极光推送等,一般指的是远程通知,使用服务器进行通知。这类通知会出现在「手机通知栏」中

(3)本地通知:比如手机中设置的闹铃时间到了,不需要使用服务器。这类通知会出现在「手机通知栏」中

本文讲的就是第三点,本地通知的使用。

 

第一步:导入头文件声明

#import <UserNotifications/UserNotifications.h>

 

第二步:初始化通知管理类获取通知权限

// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
                      completionHandler:^(BOOL granted, NSError * _Nullable error) {
    //在block中会传入布尔值granted,表示用户是否同意
    if (granted) {
        //如果用户申请权限成功,则可以设置通知中心的代理
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    }
}];

 

第三步:创建添加通知的方法

- (void)addLocalNotification:(NSString *)title subtitle:(NSString *)subtitle body:(NSString *)body {
    
    //通知内容类
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
     
    //设置通知请求发送时APP图标上显示的数字
    content.badge = @0;
    
    //设置通知的标题
    content.title = title;
    
    //设置通知的副标题
    content.subtitle = subtitle;
    
    //设置通知的内容
    content.body = body;
     
    //设置通知提示音
    content.sound = [UNNotificationSound defaultSound];
    
    //设置从通知激活App时的lanunchImage图片
    content.launchImageName = @"lanunchImage";
     
    //设置触发器
    //1-计时器触发器:ns后执行
    UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1 repeats:NO];
     
    //2-周期日历触发器
    /*
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = 2019;
    components.month = 11;
    components.day = 2;
    UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
 
    //3-地域触发器
    CLRegion *region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(33.0, 110.0) radius:100 identifier:@"region"];
    UNLocationNotificationTrigger *locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];
    */
 
    //设置通知请求
    //⚠️如果使用相同的「requestWithIdentifier」会一直覆盖之前的旧通知
    NSString *requestIdentifer = [NSString stringWithFormat:@"TestRequestww1%u",arc4random() % 1000];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:timrTrigger];
 
    //添加通知请求
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"增加一条本地通知");
        }
    }];
}

 

第四步:处理通知的delegate方法

/*
仅当应用程序在前台时,才会调用该方法。 如果未实现该方法或未及时调用该处理程序,则不会显示该通知。 应用程序可以选择将通知显示为声音,徽章,警报和/或显示在通知列表中。 该决定应基于通知中的信息是否对用户可见。
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
 
    NSLog(@"------------当前应用在前台,收到了通知消息----------------");
 
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
 
/*
当接收到通知后,在用户点击通知激活应用程序时调用这个方法,无论是在前台还是后台
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
 
    NSLog(@"------------当前应用无论是在前台还是后台,收到了通知消息,用户点击该消息----------------");
 
    completionHandler();
}

 

第五步:在需要的时候发送本地通知,比如:

/// 已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"APP生命周期:已经进入后台:%s",__func__);
    [self addLocalNotification:@"APP生命周期" subtitle:@"已经进入后台" body:@""];
}

 

标签:设置,通知,void,content,NSString,UNUserNotificationCenter,本地,UserNotifications
From: https://www.cnblogs.com/cchHers/p/17089391.html

相关文章

  • Android本地备份功能禁用
    最近部分设备激活客户端后发现本地备份的功能被禁用了,排查原因发现:/***Allowsthedeviceownertoenableordisablethebackupservice.**<p>B......
  • springboot上传资源到本地,数据库中存url
    importjava.io.File;importjava.io.IOException;importjava.net.URLEncoder;importjava.util.UUID;importorg.springframework.beans.factory.annotation.Autow......
  • dapr入门与本地托管模式尝试
    1简介Dapr是一个可移植的、事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的、无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言和......
  • Blazor入门100天 : 身份验证和授权 (5) - 本地化资源
    目录建立默认带身份验证Blazor程序角色/组件/特性/过程逻辑DB改Sqlite将自定义字段添加到用户表脚手架拉取IDS文件,本地化资源freesql生成实体类,freesql管理......
  • docker本地私有仓库与harbor私有仓库
    一、搭建本地私有仓库1、首先下载registry镜像1dockerpullregistry 2、在daemon.json文件中添加私有镜像仓库地址12345678......
  • 如何将本地的一个文件、文件夹放到gitee上
    第一步:创建一个git仓库第二步:打开安装的gitbash第三步:cd到要上传的目录下,初始化gitgitinit会在目录下创建一个.git的隐藏文件夹第四步:将文件添加到暂存区里g......
  • Idea Maven 建本地仓库-导入本地JAR包
    需求IDEA很方便集成了Maven,但是也有相应的问题,比如使用Maven仓没有包的时候不太方便,这时我们需要建立自已的本地仓库来实现实现找到Idea的安装目录下面的Maven......
  • 如何屏蔽 iOS 软件自动更新,去除更新通知和标记
    如何禁用iPhone、iPad软件自动更新。适用于iOS、iPadOS和watchOS,即iPhone、iPad和AppleWatch通用。请访问原文链接:https://sysin.org/blog/disable-ios-update/......
  • Spring Native打包本地镜像,无需通过Graal的maven插件buildtools
    简介在文章《GraalVM和SpringNative尝鲜,一步步让Springboot启动飞起来,66ms完成启动》中,我们介绍了如何使用SpringNative和buildtools插件,打包出本地镜像,也打包成Docker......
  • 本地方法栈与PC寄存器
    本地方法栈与PC程序计数器一、本地方法栈1.Java虚拟机栈用于管理Java方法的调用,而本地方法栈用于管理本地方法(一般非Java实现的方法)的调用2.本地方法栈,也是线程私有的。3.允......