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

Posted

技术标签:

【中文标题】本地通知 - 重复间隔/自定义时间不起作用【英文标题】:Local Notifications - Repeat Interval / custom time doesn't work 【发布时间】:2015-03-07 14:22:31 【问题描述】:

似乎无法让一些基本通知正常工作。

我正在使用本地通知向用户发出警报声。

我可以每小时收到一次上班通知。

我似乎无法让一个人每半小时(每 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】:

好吧,我又试了一次,但它确实可以工作,但资产没有正确导入。我刚刚删除了 finder 中的声音文件并重新导入它们,应用程序正在正确触发通知!

【讨论】:

以上是关于本地通知 - 重复间隔/自定义时间不起作用的主要内容,如果未能解决你的问题,请参考以下文章

本地通知的自定义声音不起作用

本地通知声音不起作用

重复本地通知在 react-native-push-notification 中不起作用

Ionic 3 本地通知 LED/声音在 Android 中不起作用

输入 applicationDidBecomeActive 时本地通知不起作用

自定义声音推送通知不起作用(Flutter)