iPhone发出警报,将设备从睡眠状态唤醒

时间:2011-02-22 08:25:42

标签: iphone apple-push-notifications alarm uilocalnotification

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

我可能会遗漏某些东西(我希望如此),所以有人知道,请为我澄清这个问题。闹钟,邮件和脸谱都是这样做的。

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

// 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];

1 个答案:

答案 0 :(得分:0)

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

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

- (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;
    }