我在我的应用程序中使用了本地通知,并在正确的时间成功生成了警报.
但是,当我尝试使用代码播放声音时,它无法播放....
localNot.soundName = @"Ghulam Ali-Chamkte Chaad Ko.mp3";
任何人都可以告诉我原因....声音文件的播放长度是否会影响
MyObject : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *notificationsArray;
Run Code Online (Sandbox Code Playgroud)
我在tableView中有一个MyObjects数组,可以根据名称,通知时间等进行编辑.目前,我进行了设置,所以当用户按下Save时,当前的MyObject会保存到我的DataManager的myObjectArray中.
DataManager : NSObject
@property (nonatomic, strong) NSMutableArray *myObjectArray;
Run Code Online (Sandbox Code Playgroud)
我在DataManager中调用我的方法来遍历该MyObject实例,以安排该MyObject的通知.
我认为这没问题,直到用户点击其中一个MyObjects来编辑时间,然后我才需要重新安排该对象的通知.我知道你可以得到
[[UIApplication sharedApplication] scheduledNotifications];
Run Code Online (Sandbox Code Playgroud)
但是由此,我不知道哪个通知针对哪个对象.因此,在这种情况下,最好取消整个应用程序的所有通知,然后在DataManager中为每个MyObject实例循环遍历myObjectArray,并为每个对象安排通知吗?
谢谢!
iOS应用程序可以在后台任务即将到期时安排本地通知吗?当应用程序使用NSOperationQueue进入后台时,基本上我有一些服务器端正在进行下载.
我想要的是当后台任务即将完成时通过本地通知通知用户.那么该用户可以将应用程序带到前台以保持持续的服务器数据下载
以下是我正在使用的代码,但我没有看到任何本地通知
UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
/*TO DO
prompt the user if they want to continue syncing through push notifications. This will get the user to essentially wake the app so that sync can continue.
*/
// create the notification and then set it's parameters
UILocalNotification *beginNotification = [[[UILocalNotification alloc] init] autorelease];
if (beginNotification) {
beginNotification.fireDate = [NSDate date];
beginNotification.timeZone = [NSTimeZone defaultTimeZone];
beginNotification.repeatInterval = 0;
beginNotification.alertBody = @"App is about to exit .Please bring …Run Code Online (Sandbox Code Playgroud) 我希望每天早上7点发出通知,但不会发生.我也想让它在锁屏中显示.这是我到目前为止的所有代码.
-(void)EveryDayNotify
{
NSLog(@"Good Morning Working");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setAlertBody:@"Good Morning! Have a great day!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatInterval = NSDayCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:07];
[components setMinute:0];
localNotification.fireDate = [calendar dateFromComponents:components];
[localNotification release];
}
Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的计时器只在前台运行.我在这里寻找解决方案,但我找不到任何好的解决方案.我的计时器使用Core Data,每次减量后都会保存timeInterval.此外,我发送UILocalNotification,但这不起作用.我假设它不发送警报视图,如果它在前台..这就是我现在拥有的:
-(IBAction)startTimer:(id)sender{
if (timer == nil) {
[startButton setTitle:@"Pause" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
} else {
[startButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
}
}
-(void)timerAction:(NSTimer *)t
{
if(testTask.timeInterval == 0)
{
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval--;
NSError *error;
if (![self.context save:&error]) {
NSLog(@"couldn't save: %@", [error localizedDescription]);
}
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds …Run Code Online (Sandbox Code Playgroud) 我对本地通知有这个问题,他们没有播放声音.我测试了默认的一个和自定义的那个显然是我想玩但没有!我看了很多人都面临这个问题,但他们的解决方案都没有对我有用.
UILocalNotification *local
- (IBAction)Dateaction:(id)sender {
local = [[UILocalNotification alloc]init];
local.timeZone = [NSTimeZone defaultTimeZone];
local.alertBody = [NSString stringWithFormat:@"%@ Recuerda que tienes que: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"user"], [[NSUserDefaults standardUserDefaults] objectForKey:@"selected"]];
local.fireDate = [self.DatePicker date];
/* if ([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound1"]) {
local.soundName = @"sound1.caf";
}else if([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound2"]){
local.soundName = @"sound2.caf";
}else if([[self.userdefaults objectForKey:@"sound"]isEqualToString:@"sound1"]){
local.soundName = @"sound3.caf";
}else{
local.soundName = @"sound1.caf";
}*/
local.soundName = UILocalNotificationDefaultSoundName;
}
- (IBAction)switchChanged:(id)sender {
if(self.NotSwith.isOn == YES){
[[UIApplication sharedApplication] scheduleLocalNotification:local];
}
}
Run Code Online (Sandbox Code Playgroud) UILocalNotification有一个.fireDate方法,NSDate作为一个参数然后在那时触发.但是,如果我想在我的代码中发生某些事情而不使用它的话,我该NSDate怎么办?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 11;
dateComp.day = 20;
dateComp.hour = 0;
dateComp.minute = 10;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var date:NSDate = calendar.dateFromComponents(dateComp)!
//var date:NSDate = calendar.dateFromComponents(dateComp)
var notification:UILocalNotification = UILocalNotification()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "HI, noti "
notification.fireDate = date
UIApplication.sharedApplication().scheduledLocalNotifications(notification)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() { …Run Code Online (Sandbox Code Playgroud) 我在循环中创建通知,相关代码是:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
appDelegate = self
serNotificationType = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound).union(UIUserNotificationType.Badge)
let completeAction = UIMutableUserNotificationAction()
completeAction.identifier = "COMPLETE" // the unique identifier for this action
completeAction.title = "Clear" // title for the action button
completeAction.activationMode = .Background // UIUserNotificationActivationMode.Background - don't bring app to foreground
completeAction.authenticationRequired = false // don't require unlocking before performing action
completeAction.destructive = true // display action in red
let callInAction = UIMutableUserNotificationAction()
callInAction.identifier = "CALLIN"
callInAction.title = "Call now" …Run Code Online (Sandbox Code Playgroud) 我需要向项目提供向后兼容性(iOS 9).我想出了这个:
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
} else {
// Fallback on earlier versions
}
Run Code Online (Sandbox Code Playgroud)
我应该在Fallback中写些什么?我是否需要创建本地通知实例?