gpt4 book ai didi

iphone - 在 Objective-C 中取消 UILocalnotification 的步骤

转载 作者:行者123 更新时间:2023-11-29 13:13:54 25 4
gpt4 key购买 nike

我想取消 UILocalnotification。我创建了一个名为 cancelNotification 的方法,用于取消通知。但是即使取消后我仍然收到通知我在 app delegate 中听说我们需要取消通知的几个委托(delegate)方法。任何人都可以解释我在哪里调用取消方法..如果有人可以帮助我,那就太好了

   -(UILocalNotification *)scheduleNotification :(int)remedyID
{
NSString *descriptionBody;

NSInteger frequency;

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

NSLog(@"%d",remedyID);

descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"];
frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue];

NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];

for (NSDate *fireDate in notificationFireDates)
{
notif.timeZone = [NSTimeZone defaultTimeZone];


notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;

notif.applicationIconBadgeNumber = 1;

notif.fireDate = fireDate;

NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody, @"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
nil];

notif.userInfo = userDict;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

return notif;

}

- (void)cancelNotification:(int)remedyId
{
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

for (UILocalNotification *notification in notifications)
{

int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue]; // I change the key value

NSLog(@"remedyID : %d",remedyId);
NSLog(@"notifyId : %d",notifRemedyId);
if (remedyId == notifRemedyId) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}

NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

}

最佳答案

[[UIApplication sharedApplication] cancelAllLocalNotifications];

只需将其复制在方法 didLaunchWithOptions 或 viewDidLoad 下,甚至在您的下一个 UILocalNotification 代码上。

我相信这就是您在委托(delegate)中的意思?如果您在应用启动后调用此方法,它将取消您之前所有的本地通知。

关于iphone - 在 Objective-C 中取消 UILocalnotification 的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com