删除特定的本地通知 [英] Delete a particular local notification
问题描述
我正在开发基于本地通知的iPhone闹钟应用程序。
I am developing an iPhone alarm app based on local notifications.
在删除闹钟时,相关的本地通知应该被取消。但是如何确定要取消本地通知数组中的哪个对象呢?
On deleting an alarm, the related local notification should get cancelled. But how can I determine exactly which object from the array of local notifications is to be cancelled?
我知道 [[UIApplication sharedApplication] cancelLocalNotification :通知]
方法,但我怎么能得到这个'通知'来取消呢?
I am aware of [[UIApplication sharedApplication] cancelLocalNotification:notification]
method but how can I get this 'notification' to cancel it?
推荐答案
你可以在本地通知的userinfo中保存密钥的唯一值。
获取所有本地通知,循环播放数组并删除特定通知。
You can save a unique value for key in your local notification's userinfo. Get all local notification, loop through the array and delete the particular notification.
代码如下,
OBJ-C:
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app 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
[app cancelLocalNotification:oneEvent];
break;
}
}
SWIFT:
var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications {
var notification = oneEvent as UILocalNotification
let userInfoCurrent = notification.userInfo! as [String:AnyObject]
let uid = userInfoCurrent["uid"]! as String
if uid == uidtodelete {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
UserNotification:
如果您使用 UserNotification (iOS 10+),请点击此处步骤:
If you use UserNotification (iOS 10+), just follow this steps:
-
创建UserNotification内容时,添加唯一的 identifier
使用 removePendingNotificationRequests(withIdentifiers :)
使用 removeDeliveredNotificati删除特定的已发送通知ons(withIdentifiers :)
欲了解更多信息, UNUserNotificationCenter
这篇关于删除特定的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!