在application:didFinishLaunchingWithOptions:我初始化一个UINavigationController.后来,我添加UINavigationController到窗口:
[self.window addSubview:navigationController.view]
Run Code Online (Sandbox Code Playgroud)
一切正常.现在我向我的应用添加了本地通知,当用户响应一个时,我想提出一个UIViewController.所以我想我可以覆盖application:didReceiveLocalNotification:并在那里使用我的navigationController:
[navigationController pushViewController:someVC animated:YES];
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我做了一些调试,并注意到虽然navigationController不是nil,navigationController.view但没有superview,所以我认为它没有被显示.
所以,我的问题是:我应该在哪里推动UIViewController它以便显示它?
iphone uiviewcontroller uinavigationcontroller ipad uilocalnotification
我目前正在安排本地通知,如果用户当天尚未打开应用程序,则每天下午6点出现一次.如果用户已经加载了应用程序,那么我想取消当天的通知,并在明天下午6点安排新的通知.但是,当我尝试迭代计划通知列表(这不是我在应用程序中的唯一本地通知)时,通知会正确显示,[[UIApplication sharedApplication] scheduledLocalNotifications]数组始终为空.以下是给我带来麻烦的代码段:
// See if we need to create a local notification to tell the user that they can receive a daily reward
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty
// Iterate over all the pending notifications
for( int iter = 0; iter < [notifications count]; iter++ )
{
UILocalNotification* localNotification = [notifications objectAtIndex:iter];
if( [[localNotification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] )
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents …Run Code Online (Sandbox Code Playgroud) iphone objective-c ios localnotification uilocalnotification
概观
UILocalNotificationrepeatInterval设置为NSWeekdayCalendarUnit我想做的事
repeatInterval重复,题
repeatInterval为了不重复,我应该设定的常数是多少?我似乎无法找到一个明确的线程来回答这些问题:
"当应用程序未运行时(即不在前台或后台),预定的UILocalNotification是否会触发"?
"当应用程序未运行时(即不在前台或后台)并且手机处于锁定/睡眠模式时,UILocalNotifications是否会触发?"
一些答案表明他们会像UILocalNotifications由iOS操作系统处理.有些人似乎暗示只有在应用程序在后台或前台运行时才会触发.如果手机被锁定,我无法找到有关UILocalNotifications是否有效的信息.
谢谢,詹姆斯
我在我的应用程序中使用本地通知,我唯一关心的是用户点击的确切通知.
方法
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Run Code Online (Sandbox Code Playgroud)
收到通知时触发,但我需要在用户点击时处理通知,这对我来说没用.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)
仅在应用程序未运行时触发,也不是我需要的
我在网上搜索,但发现应用程序处于活动状态或未运行时只有句柄.当应用程序在后台时,我该如何处理它?
换句话说,我需要的是识别用户在应用程序在后台运行时单击的确切通知
iphone objective-c apple-push-notifications ios uilocalnotification
我正在尝试制作闹钟应用,但当我尝试发出本地通知时,我无法播放声音.我收到了这个错误:
[user info = (null)} with a sound but haven't received permission from the user to play sounds]
这是代码:
@IBOutlet var setAlaram: UIDatePicker!
@IBAction func setAlarmButton(sender: AnyObject) {
var dateformater = NSDateFormatter()
dateformater.timeZone = NSTimeZone .defaultTimeZone()
dateformater.timeStyle = NSDateFormatterStyle.ShortStyle
dateformater.dateStyle = NSDateFormatterStyle.ShortStyle
var datetimestring = NSString()
datetimestring = dateformater.stringFromDate(setAlaram.date)
println(datetimestring)
[self .localnotification(setAlaram.date)]
}
func localnotification (firedate:NSDate) {
var localNotification:UILocalNotification = UILocalNotification()
localNotification.fireDate = firedate
localNotification.alertBody = "time to woke up"
localNotification.soundName = "alarm.wav"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
override func viewDidLoad() {
super.viewDidLoad() …Run Code Online (Sandbox Code Playgroud) 当我的应用程序进入后台时,将 自动调用applicationDidEnterBackground,并在此方法中触发本地通知.但didReceiveLocalNotification:方法未被调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试添加/安排提供两个操作的本地通知,如下所示:
如果我已经在使用手机,我还希望将通知显示为横幅提示,我可以向下滑动以查看操作.
现在,我已成功安排通知,它确实出现在锁定屏幕上.但是,当我向左滑动,然后点击"查看",我看不到我的动作.
这是我的代码:
func addNewNotificationWith (name: String, date: Date) {
let message = "You will see this, but can't do anything! lol."
let newLocalNotif = UILocalNotification()
newLocalNotif.fireDate = dueDateWarningTime
newLocalNotif.alertBody = message
newLocalNotif.timeZone = TimeZone.autoupdatingCurrent
newLocalNotif.soundName = UILocalNotificationDefaultSoundName
newLocalNotif.category = "DueDates"
let ignoreAction = getNotificationAction(identifier: "", btnTitle: "Ignore")
let doneAction = getNotificationAction(identifier: "", btnTitle: "Done")
let category = UIMutableUserNotificationCategory()
category.identifier = "DueDates"
category.setActions([ignoreAction, doneAction], for: UIUserNotificationActionContext.minimal)
let settings = UIUserNotificationSettings(types: .alert, categories: [category])
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.scheduleLocalNotification(newLocalNotif)
print("New notification added.")
} …Run Code Online (Sandbox Code Playgroud) 我在安排多个通知的for循环内安排本地通知时遇到麻烦。例如,该函数接收一个称为的变量repetition,它是一个工作日数组,目的是在该数组中的每个工作日调度通知。问题是,只有一个工作日和一个预定的通知时才会触发通知。当数组中有1个以上的项目时,不会触发任何通知。为了清晰起见,这是完整的功能:
func scheduleNotification(at date: Date, every repetition: [String], withName name: String, id: String) {
print("Scheduling notifications for the following days: \(repetition) \n \n")
var components = DateComponents()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
components.hour = hour
components.minute = minutes
for rep in repetition {
switch rep {
case "Sunday" : components.weekday = 1
case "Monday" : components.weekday = 2
case "Tuesday" : components.weekday = 3
case "Wednesday": components.weekday …Run Code Online (Sandbox Code Playgroud) 由于UILocalNotification在iOS 10中已弃用,因此我已使用UNUserNotification框架更新了本地通知流程。
当应用程序位于前台时,它会使用AVPlayer播放自定义声音,并且可以正常工作。但是在后台模式下,当触发本地通知时,会播放默认的通知声音,而不是自定义声音。
但是,在iOS9中一切正常,使用“ didReceiveLocalNotification”方法的应用程序即使在背景模式下也可以播放自定义声音。
更新1:
我将本地通知设置如下:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
if let audioUrl == nil {
content.sound = UNNotificationSound.defaultSound()
} else {
let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
}
content.userInfo = infoDic as [NSObject : AnyObject]
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let identifier = "Reminder-\(date)"
let request = UNNotificationRequest(identifier: identifier, …Run Code Online (Sandbox Code Playgroud)