gpt4 book ai didi

iphone - UILocalNotification 没有被取消

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

我正在尝试取消一个已经安排好的通知,系统正在调用通知,但是当我尝试取消一个通知时,它并没有被取消。

当只有一个计划通知时,NSArray 通知包含一些随机值。谁能帮我 。我想取消特定 bookID 的通知。

更新:

 -(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)bookID
{
int notificationBook=0;

NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

for (UILocalNotification *notification in notifications)
{
int notifBookId = [[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey] intValue];


for (int i=0; i<[bookArray count]; i++)
{
notificationBook =[[[remedyArray objectAtIndex:i] objectForKey:@"BookID"] intValue];
}

NSLog(@"%d",[[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey]intValue]);

NSLog(@"%d",notifBookId);

if (bookId == notifBookId)
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}

}

最佳答案

NSArray notification contains some random values when there is only one scheduled notification.

这可能是因为该应用程序的一些先前预定的通知已经存在。一旦尝试取消所有应用程序重新启动的通知

   [[UIApplication sharedApplication] cancelAllLocalNotifications];  

您的主要问题

- (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]);


}

您提供的 key 有误。我认为那是你的问题。我又发现一件事,你将每个通知安排两次,我不知道为什么。检查一下。

关于iphone - UILocalNotification 没有被取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16076639/

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