本地通知

1.  .m文件ios

#import "HomeViewController.h"

// 鬧鐘本地推送
// 若是鬧鐘須要設置多個通知,key就不能寫成宏定義了,須要動態生成,
// 以便在用戶關閉某個鬧鐘時,能夠移除對應的本地通知
#define KAlarmLocalNotificationKey @"KAlarmLocalNotificationKey"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
    self.title = @"本地通知";
  }
  return self;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  
  UIButton *notBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  notBtn.frame = CGRectMake(20, 100, 320-40, 40);
  [notBtn setBackgroundColor:[UIColor lightGrayColor]];
  [notBtn setTitle:@"開講啦" forState:UIControlStateNormal];
  [notBtn addTarget:self action:@selector(notClick:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:notBtn];
}

- (void)notClick:(id)sender {
  NSLog(@"notBtn:%s",__FUNCTION__);
  
  [HomeViewController registerLocalNotification:4];// 4秒後
}

// 設置本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime {
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  // 設置觸發通知的時間
  NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
  NSLog(@"fireDate=%@",fireDate);
  
  notification.fireDate = fireDate;
  // 時區
  notification.timeZone = [NSTimeZone defaultTimeZone];
  // 設置重複的間隔
  notification.repeatInterval = kCFCalendarUnitSecond;
  
  // 通知內容
  notification.alertBody =  @"該起牀了...";
  notification.applicationIconBadgeNumber = 1;
  // 通知被觸發時播放的聲音
  notification.soundName = UILocalNotificationDefaultSoundName;
  // 通知參數
  NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學習iOS開發了" forKey:@"key"];
  notification.userInfo = userDict;
  
  // ios8後,須要添加這個註冊,才能獲得受權
  if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                             categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    // 通知重複提示的單位,能夠是天、周、月
    notification.repeatInterval = NSCalendarUnitDay;
  } else {
    // 通知重複提示的單位,能夠是天、周、月
    notification.repeatInterval = NSDayCalendarUnit;
  }
  
  // 執行通知註冊
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

// 取消某個本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
  // 獲取全部本地通知數組
  NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
  
  for (UILocalNotification *notification in localNotifications) {
    NSDictionary *userInfo = notification.userInfo;
    if (userInfo) {
      // 根據設置通知參數時指定的key來獲取通知參數
      NSString *info = userInfo[key];
      
      // 若是找到須要取消的通知,則取消
      if (info != nil) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        break;
      }
    }
  }
}

@end
相關文章
相關標籤/搜索