【问题标题】:how to fire random local notification at same time everyday in ios如何在 ios 中每天同时触发随机本地通知
【发布时间】:2017-09-30 19:52:38
【问题描述】:

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

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];

【问题讨论】:

  • 添加一些你正在工作的代码。
  • @Arun 添加了代码..通过它并提出答案
  • 试试这个链接这可能对你有帮助fire local notification per day
  • 感谢您的回答,但这不是我正在寻找的解决方案。我每天重复的通知工作正常,但每次我需要更改通知的文本时,我得到的文本都是相同的
  • @RamanSrivastava 你有什么想法吗?我也被同一部分卡住了。

标签: ios objective-c iphone


【解决方案1】:

对于 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,允许在用户进入或离开指定地理区域时发送通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多