如何重复我的本地通知声音,直到我点击其“查看”按钮?

Posted

技术标签:

【中文标题】如何重复我的本地通知声音,直到我点击其“查看”按钮?【英文标题】:How Can i repeat my local notification sound repeated until i tap on its ""View" button? 【发布时间】:2011-11-02 12:59:07 【问题描述】:

通知声音为 20 秒,但我想重复该声音至少 60 秒。之后它会打盹。这是我宝贵的代码:D 看看,请帮助我...

代码:-

@interface SetAlarmViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

    IBOutlet UITableView *tableview;
    IBOutlet UIDatePicker *datePicker;
    IBOutlet UITextField *eventText;

    IBOutlet UINavigationBar *titleBar;
    IBOutlet UIButton *setAlarmButton;


@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UITextField *eventText;

@property(nonatomic, retain) IBOutlet UINavigationBar *titleBar;
@property(nonatomic, retain) IBOutlet UIButton *setAlarmButton;
@property(nonatomic) UIReturnKeyType returnKeyType;  

- (IBAction) scheduleAlarm:(id) sender;
- (void)showReminder:(NSString *)text;


@implementation SetAlarmViewController
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
    [super viewDidLoad];
    eventText.returnKeyType = UIReturnKeyDone;

    datePicker.minimumDate = [NSDate date];
    NSDate *now = [NSDate date];
    [datePicker setDate:now animated:YES];

    eventText.delegate = self;


- (void) viewWillAppear:(BOOL)animated 
    [self.tableview reloadData];


- (IBAction) scheduleAlarm:(id) sender 
    [eventText resignFirstResponder];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate = [self.datePicker date];

    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) 
                                                   fromDate:pickerDate];
    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    // Notification details
    localNotif.alertBody = [eventText text];

    // Set the action button
    localNotif.alertAction = @"Show me";
    localNotif.repeatInterval = NSDayCalendarUnit;
    localNotif.soundName = @"jet.wav";
    localNotif.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text
                                                         forKey:kRemindMeNotificationDataKey];
    localNotif.userInfo = userDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

    [self.tableview reloadData];
    eventText.text = @"";


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    return 1;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
    if(notif)
       
        [[UIApplication sharedApplication] cancelLocalNotification:notif];
    

    [self.tableview reloadData];


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    

    // Configure the cell...

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

    [cell.textLabel setText:notif.alertBody];
    [cell.detailTextLabel setText:[notif.fireDate description]];

    return cell;


- (void)viewDidUnload 
    datePicker = nil;
    tableview = nil;
    eventText = nil;
    [super viewDidUnload];



#pragma mark -
#pragma mark === Text Field Delegate ===
#pragma mark -

- (BOOL)textFieldShouldReturn:(UITextField *)textField 

    [textField resignFirstResponder];
    return YES;



#pragma mark -
#pragma mark === Public Methods ===
#pragma mark -

- (void)showReminder:(NSString *)text 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                                                        message:text delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    [self.tableview reloadData];

    [alertView release];



- (void)dealloc 
    [super dealloc];
//  [datePicker release];
//  [tableview release];
//  [eventText release];


@end

【问题讨论】:

@chown :- 我是新手,你修改了什么...? :) 刚刚修复了 1 行代码格式。 @end 没有缩进 4 个空格。 :) 好的,谢谢你……顺便说一句,我的问题有什么猜测吗……?? 【参考方案1】:

只需以the sound's duration 为间隔安排多个本地通知,直到fireDate 以来的总时间为&gt;= 60。如果它们被忽略,它们将再次关闭,如果用户按下Show Me,那么您可以在应用启动时取消安排此事件的剩余通知。

那么,剩下的唯一问题就是当他们按下Close 时会发生什么。我认为必须有一个回调才能让您在通知关闭时执行某事。对吗? (我现在找不到)

【讨论】:

@darvidsOn :- 谢谢老兄,实际上我还发现我必须提供多个通知(我们最多可以提供 128 个)我仍在考虑如何实现这一切。你的建议也很好。让我试试,如果它对我有用,我也很乐意接受答案。谢谢

以上是关于如何重复我的本地通知声音,直到我点击其“查看”按钮?的主要内容,如果未能解决你的问题,请参考以下文章

在我的 iOS 应用程序的本地通知中实现振动和声音设置

切换系统声音 - 快速

取消本地通知后如何停止本地通知声音

如何在本地通知上播放不同的声音文件

播放随机声音而不重复

前台收到远程APN推送时如何播放默认声音? [重复]