如何在localNotification上安排随机消息

时间:2018-07-13 10:49:07

标签: objective-c uilocalnotification

如何为localNotification安排随机消息。

- (void)viewDidLoad {
        // Do any additional setup after loading the view.
        [super viewDidLoad];
        [self scheduleDailyLocalNotification];
    }


-(void)scheduleDailyLocalNotification{

    UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
        localNotification.title = [NSString localizedUserNotificationStringForKey:@“Title!” arguments:nil];
srand(time(NULL));
int r = rand() % 6;
localNotification.body = [NSString localizedUserNotificationStringForKey:[self.notificationMessages objectAtIndex:r] arguments:nil];            
        localNotification.sound = [UNNotificationSound defaultSound];
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24  repeats:YES];

        //    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Time for a run!" content:localNotification trigger:trigger];

        NSString *identifier = @"LOCALNOTIFICATION";
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                              content:localNotification trigger:trigger];

        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"NOTIFICATION CREATED");
        }];
 }

它一次又一次显示相同的消息!

2 个答案:

答案 0 :(得分:1)

如果您不想再次重复此通知,请再次设置重复间隔 NO

### based on threshold = 0.045
in_b1_not_in_b2 <- c(1002.5698, 301.569)
in_b2_not_in_b1 <- c(22.12, 53, 5666.31, 100.1)

答案 1 :(得分:0)

这是执行此操作的一种方法

    var index = 0

    - (void)viewDidLoad {
            // Do any additional setup after loading the view.
            [super viewDidLoad];
            [self scheduleDailyLocalNotification];
        }


    -(void)scheduleDailyLocalNotification{

        UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
            localNotification.title = [NSString localizedUserNotificationStringForKey:@“Title!” arguments:nil];
    srand(time(NULL));
    localNotification.body = [NSString localizedUserNotificationStringForKey:[self.notificationMessages objectAtIndex:index] arguments:nil];  
        if index < self.notificationMessage.count{
             index += 1
        }else{
             index = 0
         }      
            localNotification.sound = [UNNotificationSound defaultSound];
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24  repeats:YES];

            //    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Time for a run!" content:localNotification trigger:trigger];

            NSString *identifier = @"LOCALNOTIFICATION";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                                  content:localNotification trigger:trigger];

            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                NSLog(@"NOTIFICATION CREATED");
            }];
     }