本地通知

本地通知
UILocalNotification *notification = [[UILocalNotification alloc] init];

if(notification != nil ) {
  NSDate *now = [NSDate new];
  notification.fireDate=[now dateByAddingTimeInterval:6]; //觸發(fā)通知的時(shí)間 
  notification.repeatInterval=0; //循環(huán)次數(shù),kCFCalendarUnitWeekday一周一次
  notification.soundName = UILocalNotificationDefaultSoundName;
  notification.alertBody=@"該去吃晚飯了!";
  notification.alertAction = @"打開";  //提示框按鈕

  notification.hasAction = YES; //是否顯示額外的按鈕,為no時(shí)alertAction消失 

  notification.applicationIconBadgeNumber = 1; //設(shè)置app圖標(biāo)右上角的數(shù)字

  //下面設(shè)置本地通知發(fā)送的消息,這個(gè)消息可以接受 
  NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
  notification.userInfo = infoDic;

  ////發(fā)送通知
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

接收本地消息

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
  [alert show];

  NSDictionary* dic = [[NSDictionary alloc]init];
  //這里可以接受到本地通知中心發(fā)送的消息
  dic = notification.userInfo;
  application.applicationIconBadgeNumber -= 1;
}

- (void)applicationWillResignActive:(UIApplication *)application 
{
  application.applicationIconBadgeNumber -= 1;
}

其他

- (void)viewWillAppear:(BOOL)animated{ 
  [super viewWillAppear:animated]; 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated{ 
  [super viewWillDisappear:animated]; 
  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];
}

發(fā)送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容