ios - 阻止应用程序发送通知(如果已经存在)

标签 ios uilocalnotification

我似乎找不到它,而且我不确定如何使用 Google 搜索它。在我的应用程序中,我使用后台提取来进行检查,如果有新数据,我会使用 UILocalNotification 向用户发送通知。现在,如果已经有一个通知,我该如何防止应用程序向用户发送新通知?当我几个小时不看手机时,我现在收到了 5 条相同的通知。

谢谢!

最佳答案

您可以使用 UIApplication 的属性 scheduledLocalNotifications

示例代码如下:

NSMutableArray *notifications = [[NSMutableArray alloc] init]; [notifications addObject:notification]; myApp.scheduledLocalNotifications = notifications; 
//Equivalent: [myApp setScheduledLocalNotifications:notifications];

UIApplication *myApp = [UIApplication sharedApplication];
NSArray *eventArray = [myApp scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
    if ([uid isEqualToString:uidtodelete])
    {
        //Cancelling local notification
        [myApp cancelLocalNotification:oneEvent];
        break;
    }
}

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

for (UILocalNotification *localNotification in arrayOfLocalNotifications) {

    if ([localNotification.alertBody isEqualToString:savedTitle]) {
        NSLog(@"the notification this is canceld is %@", localNotification.alertBody);

        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system

    }

}

希望这有助于找出解决方案。

关于ios - 阻止应用程序发送通知(如果已经存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802917/

相关文章:

ios 火灾前本地通知检查

ios - iOS 闹钟

ios - 在 CALayer 中放置一个全尺寸图像以适合整个屏幕

ios - 在 Swift 2 Xcode 7 Beta 4 中设置基于位置的提醒时出错

ios - Phonegap 哔声/声音 iOS 错误

iOS/核心动画 : 12 overlapping cards in a circle

ios - 向 UILocalNotification soundName 添加自定义声音

iphone - 本地通知 "didReceiveLocalNotification"调用两次

ios - 无法更改 Appdelegate 中的 rootViewController?

ios - UIButton 不会更改 touchUpInside 上的背景