首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在localNotification上调度随机消息

如何在localNotification上调度随机消息
EN

Stack Overflow用户
提问于 2018-07-13 18:49:07
回答 2查看 226关注 0票数 0

如何为localNotification安排随机消息。

代码语言:javascript
复制
- (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");
        }];
 }

它一次又一次地显示相同的信息!

EN

回答 2

Stack Overflow用户

发布于 2018-07-13 21:57:50

如果您不想一次又一次地重复此通知,请将重复间隔设置为

代码语言:javascript
复制
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 * 60 * 24  repeats:NO];
票数 1
EN

Stack Overflow用户

发布于 2018-07-13 19:55:55

这里有一种方法可以做到这一点

代码语言:javascript
复制
    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");
            }];
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51323417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档