ios - 定时显示本地通知,ios?

标签 ios calendar notifications alarm

我正在使用以下代码在固定时间显示通知。但是它不适合我的输出。

    - (IBAction)bt_set:(id)sender {

// Setting all the information about our date
int hour = 8;
int minutes = 19;
int day = 19;
int month = 10;
int year = 2014;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];

[components setDay:day];
[components setMonth:month];
[components setYear:year];
[components setMinute:minutes];
[components setHour:hour];

NSDate *myNewDate = [calendar dateFromComponents:components];
//[components release];
//[calendar release];

[self scheduleNotificationForDate:myNewDate];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note"
                                                message:@"Alarm has been set"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

  }

这是通知代码

    - (void)scheduleNotificationForDate:(NSDate *)date
  {
// Here we cancel all previously scheduled notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = date;
NSLog(@"%@",date);
NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:@"Your notification message"];
localNotification.alertAction = NSLocalizedString(@"View details", nil);

/* Here we set notification sound and badge on the app's icon "-1"
 means that number indicator on the badge will be decreased by one
 - so there will be no badge on the icon */
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

这是我在日志中得到的日期

    Notification will be shown on: 2014-10-19 02:49:00 +0000

我想这与时区有关。

我的时区是 GMT+5:30

请帮帮我

最佳答案

你有一些(NSDate *)日期----

添加以下代码以获取适当的系统时间:

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

关于ios - 定时显示本地通知,ios?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26451897/

相关文章:

ios - 处理通知回调 - IOS

android - 从安卓删除通知

java - 日历对象时区转换

xcode - 创建 OSX 通知 Xcode 4 风格

ios - 在 `UIView` 上面添加 `UIViewController`

objective-c - 重新加载数据时 MBProgressHUD 不显示

javascript - 伊斯兰(回历)日历的 Javascript Intl.DateTimeFormat() 输出在 'islamic' 和 'ar-SA' 之间存在差异

c# - .net 日历 requiredFieldValidator

ios - Objective-C 存储信息

ios - 如何使用 xmpp openfire 发送打字通知