0

有効期限を思い出させるアプリケーションに取り組んでいます。UILocalNotificationrepeat Intervalを使用して同じことを実装しました(NSMonthCalendarUnit, NSDayCalendarUnit,NSDayCalendarUnit)。たとえば、2012 年 1 月 1 日に発火日があり、繰り返し間隔は 2012 年 12 月 12 日であり、有効期限が切れるNSDayCalendarUnit可能性はありますかcancelLocalNotification:

ここにコードがあります:-

- (void) scheduleNotificationOn:(NSDate*) fireDate
                       text:(NSString*) alertText
                     action:(NSString*) alertAction
                      sound:(NSString*) soundfileName
                launchImage:(NSString*) launchImage 
                    andInfo:(NSDictionary*) userInfo


{



userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
            txtExpiryDate.text, @"ExpiryDate",
            txtRegNo.text , @"RegNo",
            nil];



UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone systemTimeZone];   
localNotification.userInfo = userInfo;
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;    


NSLog(@"Repeat Type:%@",txtRepeat.text);

if([txtRepeat.text isEqualToString:@"Every Week"])
{

    NSLog(@"Every Week");
    localNotification.repeatInterval = 256;
}

else if([txtRepeat.text isEqualToString:@"Every Month"])
{
    NSLog(@"Every Month");
    localNotification.repeatInterval = NSMonthCalendarUnit;   
}

else if([txtRepeat.text isEqualToString:@"Every Day"])

{
    NSLog(@"Every Day");
    localNotification.repeatInterval = NSDayCalendarUnit;

}


if(soundfileName == nil)
{
    localNotification.soundName = UILocalNotificationDefaultSoundName;
}
else 
{
    localNotification.soundName = soundfileName;
}

NSLog(@"appDelegate.BadgeNumber:%d",appDelegate.BadgeNumber);

localNotification.alertLaunchImage = launchImage;
appDelegate.BadgeNumber = appDelegate.BadgeNumber + 1;
localNotification.applicationIconBadgeNumber = appDelegate.BadgeNumber;    

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

[localNotification release];
}

現在の日付と有効期限を比較して作業しました。ただし、これはアプリがフォアグラウンドにあり、特定の日付のバックグラウンドではなく通知をキャンセルできない場合にのみ機能します。以下の同じコードを見つけてください:-

- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
 Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 */
    BadgeNumber = 0;   
application.applicationIconBadgeNumber = BadgeNumber;



NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSDateFormatter *formatter =[[[NSDateFormatter alloc]init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];

NSLog(@"localNotifications Count %d",localNotifications.count);


for (UILocalNotification *notify in localNotifications)
{
    //notify.applicationIconBadgeNumber = 0;

    NSString *ExpiryDateString = [notify.userInfo objectForKey:@"ExpiryDate"];
    NSDate *ExpiryDate = [formatter dateFromString:ExpiryDateString];
    NSDate * NextFireDate = nil;
    NSLog(@"Expiry Date:%@",ExpiryDateString);

    if(notify.repeatInterval == NSDayCalendarUnit)
    {
        NSLog(@"Repeat Every Day");
        NextFireDate =  [[NSDate date] dateByAddingDays:1];
        NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]); 

    }
    if(notify.repeatInterval == NSWeekCalendarUnit)
    {
        NSLog(@"Repeat Every Day");
        NextFireDate =  [[NSDate date] addTimeInterval:D_WEEK];
        NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]); 

    }
    if(notify.repeatInterval == NSMonthCalendarUnit)
    {
        NSLog(@"Repeat Every Day");
        //NextFireDate =  [[NSDate date] addTimeInterval:D_Month];
        NextFireDate = [self CalculateExipiryDateForMonth];
        NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]); 

    }


    NSComparisonResult result = [NextFireDate compare:ExpiryDate];
    NSLog(@"NSComparisonResult:%d",result);

    if(result == NSOrderedDescending)
    {

        NSLog(@"Cancell......... Notification");
        NSLog(@"notify :::%@",notify);

    }
    else 
    {
        NSLog(@"Re-Schedule Notification");
        BadgeNumber =  BadgeNumber + 1;

        notify.applicationIconBadgeNumber = BadgeNumber;

        NSLog(@"BadgeNumber:%d",BadgeNumber);

        [[UIApplication sharedApplication] scheduleLocalNotification:notify];
    }
}


}



-(NSDate*) CalculateExipiryDateForMonth
{

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
[components release];

NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nextMonth];
NSDate *expiryDay = [gregorian dateFromComponents:nextMonthComponents];


NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;

NSDate *NewExpiry = [gregorian dateByAddingComponents:dayComponent toDate:expiryDay options:0];  



[gregorian release]; 
[dayComponent release];

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSLog(@"Next Exipiry Date -----:%@",[formatter stringFromDate:NewExpiry]);


return NewExpiry;
}
4

2 に答える 2

1

つまり、アプリがバックグラウンドで実行されている間は UILocalNotification をキャンセルできません。

于 2012-09-12T13:34:14.840 に答える
0
                 **Apple RESPONSE:-**

UILocalNotification に関するご質問にお答えします。

現時点では、UILocalNotification には、通知が自動的にキャンセルされるまでの有効期限や繰り返し回数を指定する方法がありません。

現在入手できる最も近い方法は、繰り返し間隔を使用する代わりに、最大 64 個の個別の通知をスケジュールすることです。

しかし、あなたは正しいです。ユーザーがアプリをフォアグラウンドに移動しない場合、ローカル通知をキャンセルまたは再スケジュールする機会がありません。

< https://developer.apple.com/bugreporter >で拡張リクエストを提出し 、iOS の将来のリリースでこの機能を求めることを強くお勧めします。

また、ユーザーがデバイスからアプリを削除したときにローカル通知がどうなるかについても尋ねました。iOS はローカル通知を保存するため、ユーザーがアプリを削除してから再インストールした場合でもスケジュールされます。

アプリが実行されると、UIApplication のcheduledLocalNotifications プロパティを確認し、不要になった通知を削除できます。

--gc よろしくお願いします。


Garth Cummings Apple デベロッパ テクニカル サポート

于 2012-09-18T14:19:28.623 に答える