5

すべてまたは特定の通知である可能性のあるローカル通知を削除する方法に関して、あちこちいくつかの質問があることを私は知っています。また、ローカル通知クラスの参照を調べて、繰り返し時間間隔、発火日、アラートボディ、タイムゾーンなどのいくつかのメソッドを見つけました...しかし、発火日を変更する方法に関するある種の情報を見つけることができません。すでに設定されています。ユーザーが今日の日付と午後4時50分に通知を設定したが、ユーザーが設定された日付/時刻を変更したい場合は、両方の場合に通知が発生します。プログラミング倫理に関する限り、これは失敗です!

実際に私が欲しいのは、以前の通知をキャンセルする必要があります。つまり、日付を変更して編集し、通知を設定して新しい日付に配信する必要があります。

これは私が通知、サンプルコードを設定する方法です:

- (void)setNotification
{
    //Set notification after confirmation of saved data

    Class cls = NSClassFromString(@"UILocalNotification");
    reminderNotification = [[cls alloc] init];

    if (cls != nil) 
    {        
       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
       [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
       NSDate *notificationDate = [dateFormat dateFromString:textField2.text];
       reminderNotification.fireDate = notificationDate;
       reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
       NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
       reminderNotification.alertBody = reminderText;
       reminderNotification.alertAction = @"View";
       reminderNotification.soundName = @"lazy_afternoon.mp3";
       reminderNotification.applicationIconBadgeNumber = 1;
       NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];
       reminderNotification.userInfo = userDict;
       [[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
       [reminderNotification release];
    }
}

このタスクに対処する方法は?

4

2 に答える 2

18

このメソッドを使用して通知をスケジュールします。notificationIDは一意である必要があります

 -(void) scheduleNotificationForDate:(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID{

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    localNotification.timeZone = [NSTimeZone localTimeZone];
    localNotification.alertBody = alertBody;
    localNotification.alertAction = actionButtonTitle;
    localNotification.soundName = @"yourSound.wav";

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

この方法を使用して、その通知IDで特定の通知をキャンセルします

- (void)cancelLocalNotification:(NSString*)notificationID {
    //loop through all scheduled notifications and cancel the one we're looking for
    UILocalNotification *cancelThisNotification = nil;
    BOOL hasNotification = NO;

    for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[someNotification.userInfo objectForKey:notificationID] isEqualToString:notificationID]) {
            cancelThisNotification = someNotification;
            hasNotification = YES;
            break;
        }
    }
    if (hasNotification == YES) {
        NSLog(@"%@ ",cancelThisNotification);
        [[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];        
    }
}

参照: UILocalNotification

于 2012-06-21T13:14:17.163 に答える
3

通知を設定したら、それを編集する唯一の方法は、古い通知をキャンセルして別の通知を作成することです。この方法では、既存の通知を検索してキャンセルできます。

for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[aNotif.userInfo objectForKey:@"id"] isEqualToString:nId]) {
            [[UIApplication sharedApplication]cancelLocalNotification:aNotif];
        }
    }

そして、新しい通知を作成します。

于 2012-06-21T11:35:03.073 に答える