如何在祷告时间不同时间设置本地通知重复间隔?

时间:2017-05-17 06:20:27

标签: ios uilocalnotification

在本地通知中有repeatInterval属性,我们可以将单位重复间隔设置为分钟,小时,日,周,年等。

我想在祈祷时间和每天同一过程中重复间隔。

所以每个祈祷时间都会有本地通知。

祷告时间是每天不同的时间

4 个答案:

答案 0 :(得分:0)

重复你不能这样做。在接下来的30天内制作一堆不同的制作通知 - 每天一个。当用户打开应用程序时,然后为下一个30重新创建它们。

答案 1 :(得分:0)

您可以将重复间隔设置为 ,并传递不同时间的本地通知数组。

myapp.scheduledLocalNotifications = arrayOfNOtifications;

这可能会对您有所帮助:how to create multiple local notifications

答案 2 :(得分:0)

我已经完成了这样的应用程序试试这个。

func scheduleNotification() {

    let dateString = "2017-04-04 09:00:00"
    let dateFormatter = DateFormatter()

    var localTimeZoneName: String { return TimeZone.current.identifier }
    var secondsFromGMT: Int { return TimeZone.current.secondsFromGMT() }
    dateFormatter.timeZone = TimeZone(secondsFromGMT: secondsFromGMT)
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

    let dateObj:Date = dateFormatter.date(from: dateString)!



    let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: dateObj)


    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)


    let content = UNMutableNotificationContent()
    content.title = "mIdeas"
    content.body = getRandomMessage()
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "myCategory"


    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    //this commented code is to remove the pre-seted  notifications so if you need multiple times don't use this line of code
   //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in

        if let error = error {
            print("Uh oh! i had an error: \(error)")
        }
    }
}

调用此函数设置通知,您可以修改此函数并添加参数以传递时间

答案 3 :(得分:0)

是的,您可以使用重复选项在特定时间和时间推送通知。

请遵循以下代码:

//1. catch the notif center
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];



//2. I prefer removing any and all previously pending notif
[center removeAllPendingNotificationRequests];

//then check whether user has granted notif permission
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
                // Notifications not allowed, ask permission again
                [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                                      completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                          if (!error) {
                                              //request authorization succeeded!
                                          }
                                      }];
            }
        }];


//3. prepare notif content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"Hello! Today's Sunday!!",nil) arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"Sleep tight!",nil) arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

//4. next, create a weekly trigger for notif on every Sunday
NSDate* sundayDate = startDate;
NSDateComponents *components = [[NSDateComponents alloc] init];
while(true){
        NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:sundayDate];
            if(weekday == 1){//sunday, stay asleep reminder . LOL
                components.weekday = 1;
                components.hour = 9;
                components.minute = 0;
                break;
            }else{//keep adding a day
                [components setDay:1];
                sundayDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:sundayDate options:0];
            }
        }

//5. Create time for notif on Sunday
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:sundayDate];
comps.hour   = 9;
comps.minute = 0;
sundayDate = [calendar dateFromComponents:comps];

NSDateComponents *triggerWeekly = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday + NSCalendarUnitHour + NSCalendarUnitMinute fromDate:sundayDate];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerWeekly repeats:YES];

//6. finally, add it to the request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalIdentifier"                                                                            content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Local Notification succeeded");
        } else {
            NSLog(@"Local Notification failed");
        }
    }];

如果您在通知中寻找可操作的项目。你应该把它添加到中心的某个地方。

UNNotificationAction *snoozeAct = [UNNotificationAction actionWithIdentifier:@"Snooze"
                                                                           title:NSLocalizedString(@"Snooze",nil) options:UNNotificationActionOptionNone];
UNNotificationAction *deleteAct = [UNNotificationAction actionWithIdentifier:@"Delete"
                                                                           title:NSLocalizedString(@"Delete",nil) options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:identifier
                                                                              actions:@[snoozeAct,deleteAct] intentIdentifiers:@[]
                                                                              options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];
[center setNotificationCategories:categories];
content.categoryIdentifier = @"ActionIdentifier";

请务必为通知请求设置不同的标识符!