Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

ios - Dismiss an already delivered UILocalNotification?

Is it possible to do this? UIApplication's scheduledLocalNotifications doesn't seem to return notifications that have already been delivered to the user's notification center, so I think this may be by design, but I can't find any documented evidence of this.

Anyone know?

Thanks!

EDIT: Found this:

You can cancel a specific scheduled notification by calling cancelLocalNotification: on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications. Both of these methods also programmatically dismiss a currently

Here: http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html

However, how do I get a reference to an already-delivered notification, if scheduledLocalNotifications doesn't give me notifications that have already been delivered?

EDIT 2:

Here's what I'm trying to do, after I've registered some notifications:

UIApplication *app = [UIApplication sharedApplication];

for (UILocalNotification *localNotification in app.scheduledLocalNotifications) 
{
     if (someCondition) {
            [app cancelLocalNotification:localNotification];
        }
     }
}

The problem is that once they're delivered, they're no longer in 'scheduledLocalNotifications'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can solve this by adding your newly created notifications to your own NSMutableArray of notifications and check that array instead of app.scheduledLocalNotifications. Something like this:

Add a NSMutableArray to your Viewcontrollers .h file:

NSMutableArray *currentNotifications;

Initiate it when initiating your ViewController

currentNotifications = [[NSMutableArray alloc] init];

When initiating a notification, also add it to your array:

UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

Later when you want to cancel that notification, look for it in your array instead. Also remove it from your array:

for (UILocalNotification *notification in currentNotifications) {
    if (someCondition) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        [currentNotifications removeObject:notification];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...