Yog*_*ogi 87 iphone objective-c uilocalnotification usernotifications
我正在开发基于本地通知的iPhone闹钟应用程序.
删除警报时,相关的本地通知应取消.但是,如何确定要取消本地通知数组中的哪个对象呢?
我知道[[UIApplication sharedApplication] cancelLocalNotification:notification]方法,但我怎么能得到这个'通知'取消它?
Kin*_*iss 212
您可以在本地通知的userinfo中为密钥保存唯一值.获取所有本地通知,遍历数组并删除特定通知.
代码如下,
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;
}
}
Run Code Online (Sandbox Code Playgroud)
迅速:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
UserNotification:
如果您使用UserNotification(iOS 10+),请按照以下步骤操作:
创建UserNotification内容时,请添加唯一标识符
使用removePendingNotificationRequests(withIdentifiers :)删除特定的待处理通知
使用removeDeliveredNotifications(withIdentifiers :)删除特定的已发送通知
有关详细信息,请联系UNUserNotificationCenter
iMO*_*DEV 23
其他选择:
首先,当您创建本地通知时,可以将其存储在用户默认值中以供将来使用,本地通知对象不能直接存储在用户默认值中,此对象需要先转换为NSData对象,然后NSData才能存入User defaults.以下是代码:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%d",indexPath.row]];
Run Code Online (Sandbox Code Playgroud)
存储和计划本地通知后,将来可能需要取消您之前创建的任何通知,因此您可以从用户默认值中检索它.
NSData *data= [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%d",UniqueKey]];
UILocalNotification *localNotif = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"Remove localnotification are %@", localNotif);
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%d",UniqueKey]];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
小智 8
这就是我做的.
创建通知时,请执行以下操作:
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = alertDate;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertAction = NSLocalizedString(@"Start", @"Start");
notification.alertBody = **notificationTitle**;
notification.repeatInterval= NSMinuteCalendarUnit;
notification.soundName=UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
Run Code Online (Sandbox Code Playgroud)
当试图删除它时,请执行以下操作:
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
}
}
Run Code Online (Sandbox Code Playgroud)
此解决方案应适用于多个通知,并且您不管理任何阵列或字典或用户默认值.您只需使用已保存到系统通知数据库的数据即可.
希望这有助于未来的设计师和开发者
快乐的编码家伙!:d
在swift中调度和删除通知:
static func scheduleNotification(notificationTitle:String, objectId:String) {
var localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 24*60*60)
localNotification.alertBody = notificationTitle
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.applicationIconBadgeNumber = 1
//play a sound
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertAction = "View"
var infoDict : Dictionary<String,String!> = ["objectId" : objectId]
localNotification.userInfo = infoDict;
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
static func removeNotification(objectId:String) {
var app:UIApplication = UIApplication.sharedApplication()
for event in app.scheduledLocalNotifications {
var notification = event as! UILocalNotification
var userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>
var infoDict : Dictionary = notification.userInfo as! Dictionary<String,String!>
var notifcationObjectId : String = infoDict["objectId"]!
if notifcationObjectId == objectId {
app.cancelLocalNotification(notification)
}
}
}
Run Code Online (Sandbox Code Playgroud)
iMOBDEV的解决方案可以完美地删除特定通知(例如,在删除警报后),但是当您需要有选择地删除任何已经触发但仍在通知中心的通知时,它特别有用.
可能的情况是:警报的通知触发,但用户打开应用程序而不点击该通知并再次安排该警报.如果您想确保给定项目/警报的通知中心只能有一个通知,这是一个很好的方法.它还允许您不必在每次打开应用程序时清除所有通知,这样可以更好地适应应用程序.
NSKeyedArchiver它存储为Data在UserDefaults.您可以创建一个等于您在通知的userInfo字典中保存的密钥.如果它与Core Data对象关联,则可以使用其唯一的objectID属性.NSKeyedUnarchiver.现在您可以使用cancelLocalNotification方法删除它.UserDefaults.这是该解决方案的Swift 3.1版本(适用于iOS 10以下的目标):
商店
// localNotification is the UILocalNotification you've just set up
UIApplication.shared.scheduleLocalNotification(localNotification)
let notificationData = NSKeyedArchiver.archivedData(withRootObject: localNotification)
UserDefaults.standard.set(notificationData, forKey: "someKeyChosenByYou")
Run Code Online (Sandbox Code Playgroud)
检索并删除
let userDefaults = UserDefaults.standard
if let existingNotificationData = userDefaults.object(forKey: "someKeyChosenByYou") as? Data,
let existingNotification = NSKeyedUnarchiver.unarchiveObject(with: existingNotificationData) as? UILocalNotification {
// Cancel notification if scheduled, delete it from notification center if already delivered
UIApplication.shared.cancelLocalNotification(existingNotification)
// Clean up
userDefaults.removeObject(forKey: "someKeyChosenByYou")
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 4 解决方案:
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
for request in requests {
if request.identifier == "identifier" {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["identifier"])
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64225 次 |
| 最近记录: |