iOS开发中,实现本地推送

本文详细介绍了如何在iOS8中实现本地推送功能,包括创建本地通知、设置通知属性及发送通知的方法。此外还讲解了如何根据项目需求选择不同的发送方式,并提供了关于通知栏显示和应用角标的管理建议。

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

一.iOS8本地推送注册
//创建本地通知
- (void)requestAuthor
{
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
    // 设置通知的类型可以为弹窗提示,声音提示,应用图标数字提示
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        // 授权通知
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //本地推送
    [self requestAuthor];
    return YES;
}
 
三.假设在ViewController中添加touchesBegan方法,具体UILocalNotification的基本属性请往下看!
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.创建通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    // 2.设置通知的必选参数
    // 设置通知显示的内容
    localNotification.alertBody = @"本地通知测试";
    // 设置通知的发送时间,单位秒
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    //解锁滑动时的事件
    localNotification.alertAction = @"别磨蹭了!";
    //收到通知时App icon的角标
    localNotification.applicationIconBadgeNumber = 1;
    //推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 3.发送通知(? : 根据项目需要使用)
    // 方式一: 根据通知的发送时间(fireDate)发送通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 
    // 方式二: 立即发送通知
    // [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
 
注意:UILocalNotification的基本属性
fireDate:启动时间;
timeZone:启动时间参考的时区;
repeatInterval:重复推送时间(NSCalendarUnit类型),0代表不重复;
repeatCalendar:重复推送时间(NSCalendar类型);
alertBody:通知内容;
alertAction:解锁滑动时的事件;
alertLaunchImage:启动图片,设置此字段点击通知时会显示该图片;
alertTitle:通知标题,适用iOS8.2之后;
applicationIconBadgeNumber:收到通知时App icon的角标;
soundName:推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName;
userInfo:发送通知时附加的内容;
category:此属性和注册通知类型时有关联,(有兴趣的同学自己了解,不详细叙述)适用iOS8.0之后;
region:带有定位的推送相关属性,具体使用见下面【带有定位的本地推送】适用iOS8.0之后;
regionTriggersOnce:带有定位的推送相关属性,具体使用见下面【带有定位的本地推送】适用iOS8.0之后;
 
四.注意一点. 当再次进入app中,通知栏的通知需要不显示,并且app的角标也要没有,所以需要在appDelegate设置一个属性.
- (void)applicationWillEnterForeground:(UIApplication *)application {
    //设置应用程序图片右上角的数字(如果想要取消右上角的数字, 直接把这个参数值为0)
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
 

转载于:https://www.cnblogs.com/KennyHito/p/6860242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值