如何在ios中每天同时发送随机本地通知

时间:2017-05-03 06:09:49

标签: ios objective-c iphone

我正在开发一款需要本地通知的应用。我有一个包含多个对象的文本数组,我需要每天触发数组的每个元素。 我在这面临很多问题。每次只从阵列中触发一个通知。 帮助我。

UILocalNotification * notification = [[UILocalNotification alloc] init];

        notification.fireDate = fireDateOfNotification;
        notification.timeZone = [NSTimeZone localTimeZone];
        NSString *myString = SelectedAffirmationText;
        NSArray *myArray = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
        //NSString *StrForNotification=[myArray objectAtIndex:0];

        for(int i=0;i<myArray.count;i++)
        {
          NSString *StrForNotification=[myArray objectAtIndex:i];
         notification.alertBody = [NSString stringWithFormat: @"%@",StrForNotification];
        }


        notification.alertAction = @"go back";
        NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:DateSelected, @"Date", TimeSelected, @"Time",SelectedAffirmationText,@"DataAffiramtion", nil];

        notification.userInfo = userDict;
        notification.repeatInterval= NSDayCalendarUnit;
        notification.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

1 个答案:

答案 0 :(得分:0)

对于iOS 10.0,为您的本地通知添加时间,如下所示。您必须添加通知框架

var date = DateComponents()
date.hour = 22
let calendarTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

创建如下内容

let content = UNMutableNotificationContent()
content.title = "Title"
content.subtitle = "Subtitle"
content.body = "Some  body."
content.badge = 1
content.sound = UNNotificationSound.default()

现在创建UNNotificationRequest并传递内容,触发。

目前,UserNotifications框架为您提供了三个:

UNTimeIntervalNotificationTrigger,允许在安排通知后发送通知一段时间。

UNCalendarNotificationTrigger,允许在特定日期和时间发送通知,无论何时安排通知。

UNLocationNotificationTrigger,允许在用户进入或离开指定地理区域时发送通知。