用声音将设备从睡眠中唤醒的 iPhone 闹钟 [英] iPhone alarms that wake up the device from sleep with sound

查看:24
本文介绍了用声音将设备从睡眠中唤醒的 iPhone 闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iPhone 的闹钟功能(本地通知)有点困惑,我还没有找到明确的答案.我想创建闹钟(甚至新邮件)等功能.具体来说,如果设备处于睡眠状态,它会被嗡嗡声或声音唤醒.您看不到的弹出消息(因为设备处于睡眠状态)的用处要小得多.但是,使用 UILocalNotification 服务似乎不会发生这种情况.我还没有检查推送通知,但它们似乎是为了别的东西.

I'm a bit confused on iPhone's capabilities for alarms (local notifications) and I haven't found a clear answer yet. I would like to create functionality like the alarm clock (or even new mail). Specifically, if the device is asleep, it gets waken with a buzz or sound. A popup message that you can't see (because the device is asleep) is a lot less useful. But, it seems that using the UILocalNotification service, this doesn't seem to be happening. I haven't checked out push notifications, but they seem to be for something else.

我可能遗漏了一些东西(我希望如此),所以请知道的人为我澄清这个问题.闹钟、邮件和 Facebook 都可以做到这一点.

I'm may be missing something (and I'm hoping so), so someone that knows, please clarify this issue for me. The alarm clock, mail and facebook all do this.

我现在正在做的代码片段:

Code snippet of what I'm doing now:

// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];

alarm.fireDate = itemDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = NSDayCalendarUnit;
alarm.soundName = @"alarmsound2.m4a";
alarm.alertBody = NSLocalizedString(@"WakeUp", @"");
alarm.hasAction = YES;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"alarm_notify" forKey:@"type"];
alarm.userInfo = infoDict;
[app scheduleLocalNotification:alarm];
[alarm release];

推荐答案

我刚刚在 AppDelegate 中使用以下代码创建了一个示例应用程序,它按预期工作.当手机处于睡眠模式时,我会收到带有默认声音和警报的通知.

I just created a sample application with the following code in the AppDelegate and it working as expected. I get a notification with default sound and alert when the phone is in sleep mode.

请注意,本地通知仅适用于 iOS 4.0 或更高版本.

Please note that the Local notifications only work with iOS 4.0 or later.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // Override point for customization after application launch.

      UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
      localNotification.soundName = UILocalNotificationDefaultSoundName;
      localNotification.alertBody = @"Local Notification Body : Some Alert";
      localNotification.alertAction = @"Action String";

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      [localNotification release];


      self.window.rootViewController = self.viewController;
      [self.window makeKeyAndVisible];
      return YES;
    }

这篇关于用声音将设备从睡眠中唤醒的 iPhone 闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆