首页 文章

本地通知 - 重复间隔/自定义时间不起作用

提问于
浏览
0

似乎无法获得一些基本的通知工作 .

我正在使用本地通知将警报声音发送给用户 .

我可以每小时收到一份工作通知 .

我似乎无法每半小时(每30分钟,或30分钟或1小时)或每15分钟工作一次 .

如何为每小时重复间隔设置一个偏移量?通知未触发 .

如果这是不可能的,我将如何手动编写一个循环,直到64个排队通知的限制?

UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
NSInteger hour = [components hour];

NSDate *h = [calendar dateBySettingHour:hour+1 minute:0 second:0 ofDate:[NSDate date] options:0];
     NSDate *f = [calendar dateBySettingHour:hour minute:15 second:0 ofDate:[NSDate date] options:0];
     NSDate *ha = [calendar dateBySettingHour:hour minute:30 second:0 ofDate:[NSDate date] options:0];
     NSDate *q = [calendar dateBySettingHour:hour minute:45 second:0 ofDate:[NSDate date] options:0];
    if (selected == 0)
        {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                if (notification)
                {
                    notification.repeatInterval = NSCalendarUnitHour;
                    notification.alertBody = nil;
                    notification.alertAction = nil;
                    notification.soundName = @"4.mp3";
                    notification.fireDate = h;
                    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
                }
        }
        else if (selected == 1)
        {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            if (notification)
            {
                notification.fireDate = ha;
                notification.repeatInterval = NSCalendarUnitHour;
                notification.alertBody = nil;
                notification.alertAction = nil;
                notification.soundName = @"2.mp3";
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
                notification.soundName = @"4.mp3";
                notification.fireDate = h;
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            }
        }
        else
        {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            if (notification)
            {
                notification.fireDate = f;
                notification.repeatInterval = NSCalendarUnitHour;
                notification.alertBody = nil;
                notification.alertAction = nil;

                notification.soundName = @"1.mp3";
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];

                notification.soundName = @"2.mp3";
                notification.fireDate = ha;
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];

                notification.soundName = @"3.mp3";
                notification.fireDate = q;
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];

                notification.soundName = @"4.mp3";
                notification.fireDate = h;
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            }
        }

1 回答

  • 0

    好吧,我再试一次,但它确实有效,但资产没有正确导入 . 我刚刚在finder中删除了声音文件,并重新导入了它们,应用程序正在解开通知!

相关问题