实用知识:本地推送的方法使用

本文详细介绍了如何在iOS应用中实现本地通知,包括通知的基本配置、触发方式、内容填充及权限管理。通过实例展示了如何创建定时提醒功能,使开发者能够轻松地为用户提供及时的通知服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 使用通知, 需要获取授权
    // 从 UIApplication 类当中找到授权

    /**
     UIUserNotificationTypeNone    = 0,      没有
     UIUserNotificationTypeBadge   = 1 << 0, 图标边缘数字
     UIUserNotificationTypeSound   = 1 << 1, 通知提示声音
     UIUserNotificationTypeAlert   = 1 << 2, 弹窗 (横幅/AlertView)
     */
    UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

    // UIUserNotificationSettings 表示App可以使用的通知的展示类型(声音, 边缘数字, 弹窗)
    // category 是用来配置 按钮的
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];

    // 注册用户通知的配置(通知使用的类型), (会弹出授权窗口来请求)
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"来了");

    // 需要手动的把 applicationIconBadgeNumber 设置为0 , 才能让边缘数字消失
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

@end
#import "ViewController.h"

#define MyUserLocalNotificationIdentifer @"MyUserLocalNotificationIdentifer"

@interface ViewController ()

// 移除指定的通知时, 使用属性来记录是不正确的
//@property (strong, nonatomic) UILocalNotification *notification;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)btnAction:(id)sender
{
    // 使用通知, 用户授权

    /*================= 召召, 补*, 定时提醒, 本地通知 =================*/

    // UILocalNotification 表示本地通知, 两种触发类型: 时间 / 区域
    // 1. 实例化
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 2. 给通知配置内容
    // 2.1 通知的触发时间, 4秒后触发
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4];
    // 2.2 通知的内容
    notification.alertBody = @"天气好晴朗, 该出去走走了";
    // 2.3 通知的标题, 显示在通知中心/Watch的通知, 横幅上的名字是App名字
    notification.alertTitle = @"要吃糖";
    // 2.4 通知的提示音, UILocalNotificationDefaultSoundName默认提示音, 模拟器默认没声音
//    notification.soundName = UILocalNotificationDefaultSoundName;
    // 使用在bundle当中的音效文件的名字,  .mp3, .aac, .wav
    notification.soundName = @"buyao.wav";
    // 2.5 图标边缘数字, 需要手动修改回来, 否则一直为6
    notification.applicationIconBadgeNumber = 6;

    // 2.6 通知的重复间隙(重复执行) 最小是单位是分钟, 配置了会重复执行
    // 默认只执行一次
    // 重复执行的, 在执行完一次后并不会从通知调试池中调出
    notification.repeatInterval = NSCalendarUnitMinute;

    // 2.7 通过userInfo来携带我们需要的数据
    notification.userInfo = @{
                              MyUserLocalNotificationIdentifer : @"ZhaoZhao"
                              };


    // 3. 等待被触发 (UIApplication)
    // 将通知添加到 通知调度池, 等待被调度(scheduleLocal)
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

- (IBAction)seeBtnAction:(id)sender
{
    // 获取当前通知调试池当中的所有通知
    NSArray <UILocalNotification *> *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

    NSLog(@"%@", notifications);
}

// 移除指定的通知
- (IBAction)removeBtnAction:(id)sender
{
    // 移除指定通知时, 不能使用属性来记录

    // 1. 去通知调试池当中查找, 查找到要移除的通知
    NSArray <UILocalNotification *> *notications = [[UIApplication sharedApplication] scheduledLocalNotifications];

    for (UILocalNotification *notification in notications) {
        // 判断条件
        [notification.userInfo[MyUserLocalNotificationIdentifer] isEqualToString:@"ZhaoZhao"];

        // 2. 移除
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}

- (IBAction)removeAllBtnAction:(id)sender
{
    // 移除所有的本地通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值