首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止本地通知

停止本地通知
EN

Stack Overflow用户
提问于 2012-07-23 07:28:21
回答 2查看 3.4K关注 0票数 0

以下是我的一个问题:

  • 当通过按“主页”按钮最小化应用程序到后台时,每5分钟创建一次弹出的本地通知。
  • 从背景中删除应用程序。->我的预期

我的问题是,本地通知仍然处于活动状态,并且在从背景中删除后每5分钟仍会弹出一次。

我怎么才能阻止它?请帮帮我!谢谢你的进阶。

EN

回答 2

Stack Overflow用户

发布于 2012-07-23 07:34:13

将其放入应用程序委托中。当应用程序进入后台时,它将删除所有本地通知。

代码语言:javascript
复制
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
票数 2
EN

Stack Overflow用户

发布于 2012-07-23 08:48:30

如果你不想取消所有通知..。我已经设置了一个存储在通知的userInfo字典中的唯一标识符。当我想要删除时,我会快速枚举所有通知,并选择正确的通知进行删除。

这里的绊脚石是记得存储我为通知创建的UUID,还记得在快速枚举中使用isEqualToString。我想我也可以使用一个特定的名称字符串,而不是唯一的标识符。如果有人能让我知道一个比快速枚举更好的方法,请告诉我。

代码语言:javascript
复制
@interface myApp () {
    NSString *storedUUIDString; 
}

- (void)viewDidLoad {
    // create a unique identifier - place this anywhere but don't forget it! You need it to identify the local notification later
    storedUUIDString = [self createUUID]; // see method lower down
}

// Create the local notification
- (void)createLocalNotification {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil) return;
    localNotif.fireDate = [self.timerPrototype fireDate];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = @"Hello world";
    localNotif.alertAction = @"View"; // Set the action button
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:storedUUIDString forKey:@"UUID"];
    localNotif.userInfo = infoDict;

    // Schedule the notification and start the timer
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
}

// Delete the specific local notification
- (void) deleteLocalNotification { 
// Fast enumerate to pick out the local notification with the correct UUID
    for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {        
    if ([[localNotification.userInfo valueForKey:@"UUID"] isEqualToString: storedUUIDString]) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system            
        }
    }
}

// Create a unique identifier to allow the local notification to be identified
- (NSString *)createUUID {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return (__bridge NSString *)string;
}

在过去6个月的某个时候,上述的大部分可能已经从StackOverflow上取消了。希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11608087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档