如何在 iOS (ObjC) 中安排每日本地推送通知?

2024-04-28

无法安排每日本地PushNotification以正确的方式。 我只想在上午 9:00 显示一个每日本地 PushNotification,其中包含今天的计数任务。

我的代码只执行一次didFinishLaunchingWithOptions like:

- (void)scheduleLocalNotification
{
    self.localNotification = [UILocalNotification new];

    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    components.hour = 9;
    components.minute = 0;
    components.second = 0;
    NSDate *fireTime = [calendar dateFromComponents:components];

    _localNotification.fireDate = fireTime;
    _localNotification.alertBody = [NSString stringWithFormat:@"Hi, <username>. You have %ld tasks for today", (long)_todayTasks];
    _localNotification.repeatInterval = NSCalendarUnitDay;
    _localNotification.soundName = @"alarm.wav";
    _localNotification.timeZone = [NSTimeZone localTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NotificationManagerLog(@"schedule local notification at %@", [dateFormatter stringFromDate:fireTime]);
}

看起来我错过了一些东西,因为它确实是在 9:00 触发的,但是数据错误_todayTasks,也可以与其他随机触发_todayTasks值并随机重复几次。


一步步:

  1. 注册通知:

    - (void)prepareLocalNotification
        {
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        } else  {
            UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
        }
    }
    
  2. 如果用户允许本地通知,则创建通知(例如每天下午 3 点):

    - (void)createDailyBasisNotification
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        UILocalNotification *next3pm = [self notificationForTime:15 withMessage:<NotificationMessage3PM> soundName:UILocalNotificationDefaultSoundName];
    
        [[UIApplication sharedApplication] scheduleLocalNotification:next3pm];
    }
    
    - (UILocalNotification *)notificationForTime:(NSInteger)time withMessage:(NSString *)message soundName:(NSString *)soundName
    {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = message;
        localNotification.repeatInterval = NSDayCalendarUnit;
        localNotification.timeZone = [NSTimeZone localTimeZone];
        localNotification.soundName = soundName;
    
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    
        [dateComponents setHour:time];
    
        NSDate *next = [calendar dateFromComponents:dateComponents];
        if ([next timeIntervalSinceNow] < 0) {
            next = [next dateByAddingTimeInterval:60*60*24];
        }
        localNotification.fireDate = next;
    
        return  localNotification;
    }
    

也不要忘记打电话[[UIApplication sharedApplication] cancelAllLocalNotifications];- 这将清除所有通知,您将删除所有通知random通知

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS (ObjC) 中安排每日本地推送通知? 的相关文章

  • 动画导航控制器“后退”按钮

    我在导航控制器层次结构中的视图控制器上有一个自定义按钮 按下该按钮时 会弹出可见的视图控制器 我想使用 UIView 的transform属性来动画关闭视图控制器 它有效 但如果我使用 popViewControllerAnimated Y
  • 在 Swift 中使用 Obj-C 完成块

    在 Objective C 中 我有一个完成块类定义为 File h typedef void MYCompletionBlock BOOL success NSDictionary result NSError error 然后 在 Sw
  • 关闭应用程序后如何调试

    我正在尝试重现问题 这需要在特定位置关闭并重新打开我的应用程序 这是我的问题 1 如何查看我的日志 使用NSLog命令 当我的 iPhone 未连接到 XCode 时 2 是否可以将iPhone模拟器的特定位置 例如市中心 设置为默认位置
  • iOS 中的视频可以进行反卷积吗?

    我想拍摄击球手挥动棒球的镜头 但球棒很模糊 视频为 30 fps 通过研究 我发现反卷积似乎是最小化运动模糊的方法 但我不知道是否或如何在我的 iOS 应用程序后处理中实现它 我希望有人能给我指出正确的方向 比如如何在 iOS 中应用反卷积
  • 如何声明仅调试语句

    在 C 中 我可以使用以下代码来获得仅在调试构建期间执行的代码 我如何在 Xcode 中执行相同的操作 if DEBUG etc etc endif 您可以使用 ifdef DEBUG endif 你需要添加DEBUG 1到调试配置设置中项
  • 当 UIView 通过自动布局调整大小时,会调用什么方法?

    我有一个图像视图 我通过在子类中覆盖以下内容来实现圆角 void setFrame CGRect frame super setFrame frame self layout setCornerRadius frame size width
  • 为什么分割视图控制器必须始终是您创建的任何界面的根?

    在苹果的开发者指南中 他们指出 分割视图控制器必须始终是您创建的任何界面的根 see here http developer apple com library ios featuredarticles ViewControllerPGfo
  • 为什么每次用户启动应用程序时都要注册推送通知?

    在Apple文档中你可以找到下面这句话 应用程序应在每次启动时进行注册 并向其提供者提供当前令牌 它调用 registerForRemoteNotificationTypes 来启动注册过程 因此 当我在应用程序中实现推送通知时 我必须注册
  • SwiftUI 转义闭包捕获变异的“self”参数

    我有一个可以通过两种方式打开的视图 一个包含提供给它的数据 另一个包含对 Firestore 文档的文档引用 我创建了两个构造函数 在第一个构造函数中我提供数据 在另一个构造函数中我提供文档参考 然后我使用此引用进行网络调用 但出现错误 E
  • Xcode 8.2.1 无法添加文件夹引用

    我的 Xcode 8 2 1 不允许我添加文件夹引用 我尝试过的 我右键单击名为 Unity 的文件夹 然后单击 将文件添加到项目 在选项下选中 创建文件夹引用 我的文件夹被添加为红色文件 而不是通常的蓝色文件夹 我尝试将文件的 类型 更改
  • iOS图片上的线测量

    我需要一些帮助来开始绘制末端带有圆圈的线条 并测量其长度 我能够划清界限 但无法让它移动 花了几个小时决定在 SO 上发布 因此 请参阅下图并指导我开始 任何使用 Objective C 的示例或教程都会有帮助 谢谢 这个想法看起来实现起来
  • 从 Xcode 6 安装失败:“存在内部 API 错误”

    我尝试在 ipad ios 7 1 2 上运行一个在我的手机 ios 8 4 1 上运行良好的应用程序 Xcode 提示 存在内部 API 错误 仅此而已 我不确定如何解释日志 怎么了 我该如何解决 ipad日志 Aug 29 17 39
  • 如何在 Swift 中将 Int 转换为字符

    我在这里挣扎了十多分钟 失败了 我屈服了 我需要在 Swift 中将 Int 转换为 Character 但无法解决它 Question 你如何转换 cast an Int integer to a Character char 在斯威夫特
  • 自调整大小的集合视图在 iOS 15 中进入递归循环

    我有一个自动调整大小的集合视图 当我调用 super layoutSubviews 时 我的应用程序崩溃 因为集合视图进入递归更新循环 这在 iOS 14 及更低版本中运行良好 但从 iOS 15 开始就观察到了这一点 class Dyna
  • 我可以更改导航栏项目的位置吗?

    Here is the snapshot 代码在这里 UIButton leftButton UIButton buttonWithType UIButtonTypeCustom leftButton frame CGRectMake 0
  • Google Maps iOS SDK:地图上东西点之间的距离

    如何计算地图东点和西点之间的距离 以米为单位 假设用户更改了滚动地图的位置 然后我使用 mapView didChangeCameraPosition delegate 方法捕获移动 但我不知道如何计算距离 这是一个辅助函数 可用于计算两个
  • 如何在 Objective-C 标头中引用 Swift 枚举

    有没有办法从 Objective C 标头引用 Swift 枚举 如果你想在 Objective C 头中查看 Swift 类 你可以使用 objc class Foo 我没有看到枚举有任何类似的东西 你想做的事情叫做forward dec
  • NSDate 休息 5 小时

    我运行以下代码 NSDate now NSDate 日期 NSLog now now 并得到 2011 09 16 16 14 16 434 iSavemore 1229 7907 现在 2011 09 16 21 14 16 0000 如
  • 呈现 UIActivityViewController 时发出警告

    当我提出一个UIActivityController使用我得到的下面的代码 它被呈现 但控制台显示 Warning Attempt to present
  • ios - ARKit - 如何创建旋转对象手势功能?

    我是 ARKit 新手 我想创建一个旋转对象的函数 这是我关于拖动和旋转对象的代码 Rotate object objc func rotateRecognized sender UIPanGestureRecognizer let sce

随机推荐