gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:37:42 25 4
gpt4 key购买 nike

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

我的代码只在 didFinishLaunchingWithOptions 中执行一次,例如:

- (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 通知

关于ios - 如何在 iOS (ObjC) 中安排每日本地推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com