Tôi muốn nhận thông báo cục bộ ngẫu nhiên (Văn bản và Âm thanh) trong từng phút. Tôi đang sử dụng dưới đây mã:Thông báo địa phương ngẫu nhiên trong nền
self.randomIndex_text = arc4random() % [self.array_motivation count];
self.randomIndex_alarm = arc4random() % [self.array_alarm count];
NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);
Mã này một cách hoàn hảo làm việc cho báo
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
notif.soundName = [NSString stringWithFormat:@"%@.mp3", [self.array_alarm objectAtIndex:self.randomIndex_alarm]];
[self _showAlert:[NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]] withTitle:@"Daily Achiever"];
}
hiển thị từ trên mã và trên Ok cảnh báo dưới đây này gọi phương thức:
-(void)insert:(NSDate *)fire
{
self.localNotification = [[UILocalNotification alloc] init];
if (self.localNotification == nil)
return;
self.randomIndex_text = arc4random() % [self.array_motivation count];
self.randomIndex_alarm = arc4random() % [self.array_alarm count];
NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);
self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:refTimeIntrval];
self.localNotification.timeZone = [NSTimeZone defaultTimeZone];
self.localNotification.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]];
self.localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]];
self.localNotification.alertAction = @"View";
self.localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
self.localNotification.repeatInterval=NSMinuteCalendarUnit;
NSLog(@"alertBody %@,soundName %@", self.localNotification.alertBody, self.localNotification.soundName);
[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
}
nhưng không không hoạt động ở chế độ nền. Tôi chỉ cần đặt này trên phương pháp ngẫu nhiên trong
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif)
{
self.randomIndex_text = arc4random() % [self.array_motivation count];
self.randomIndex_alarm = arc4random() % [self.array_alarm count];
NSLog(@"tempmethod text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:refTimeIntrval];
localNotif.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]];
localNotif.soundName =[NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]];
localNotif.alertAction = NSLocalizedString(@"Read Msg", nil);
localNotif.applicationIconBadgeNumber = 1;
[localNotif setRepeatInterval:NSMinuteCalendarUnit];
[application presentLocalNotificationNow:localNotif];
NSLog(@"sound: %@, alertAction: %@, alerBody: %@, ref: %f, str_time: %@",localNotif.soundName, localNotif.alertAction, localNotif.alertBody, refTimeIntrval, str_Time);
[self performSelector:@selector(bgmethodd) withObject:nil afterDelay:refTimeIntrval];
break;
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
NSLog(@"smh: %d,%d,%d",self.seconds, self.minutes, self.hours);
}
}
hơn Một điều tôi nhận thấy khi tôi gỡ lỗi rằng applicationDidEnterBackground
cuộc gọi tại một lần duy nhất (nghĩa là khi di chuyển ứng dụng trong nền). Sau đó không có bất kỳ phương pháp gọi cho đến khi ứng dụng mở một lần nữa nhưng tôi vẫn nhận được văn bản thông báo và continoulsy âm thanh. Nhưng văn bản và âm thanh này không phải là ngẫu nhiên.
Hãy gợi ý cho tôi một số ý tưởng và chia sẻ kiến thức của bạn rằng từ đó văn bản thông báo và âm thanh đến khi không có bất kỳ phương thức nào gọi trong nền. Và nó có thể làm cho thông báo ngẫu nhiên trong nền. Cảm ơn trước.
Đặt mã nguồn đầy đủ của bạn vào đây để '- (void) applicationDidEnterBackground: (UIApplication *) application' và' - (void) ứng dụng: (UIApplication *) ứng dụng didReceiveLocalNotification: (UILocalNotification *) Th.báo ' – viral
Behavior cho '- (void) applicationDidEnterBackground: (UIApplication *) application', bạn đã đề cập trong câu hỏi của bạn là hoàn toàn bình thường. – viral
Có thể nhận thông báo ngẫu nhiên trong nền không? Và tôi đang sử dụng cùng một mã như dưới đây liên kết: iphonesdkdev.blogspot.in/2010/04/local-push-notification-sample-code-os.html – Abha