iOS8本地推送

weixin_33670713發表於2015-12-14
  • 在IOS8下沒有註冊,所以需要額外新增對IOS8的註冊方法

第一步:註冊本地通知

// 設定本地通知  
+ (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];  
}  

第二步:處理通知,這個是在appdelegate中的代理 方法回撥

// 本地通知回撥函式,當應用程式在前臺時呼叫  
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {  
  NSLog(@"noti:%@",notification);  
    
  // 這裡真實需要處理互動的地方  
  // 獲取通知所帶的資料  
  NSString *notMess = [notification.userInfo objectForKey:@"key"];  
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前臺)"  
                                                  message:notMess  
                                                 delegate:nil  
                                        cancelButtonTitle:@"OK"  
                                        otherButtonTitles:nil];  
  [alert show];  
    
  // 更新顯示的徽章個數  
  NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;  
  badge--;  
  badge = badge >= 0 ? badge : 0;  
  [UIApplication sharedApplication].applicationIconBadgeNumber = badge;  
    
  // 在不需要再推送時,可以取消推送  
  [HomeViewController cancelLocalNotificationWithKey:@"key"];  
} 

第三步:在需要的時候取消某個推送

// 取消某個本地推送通知  
+ (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;  
      }  
    }  
  }  
}  

示例圖:


987457-0427673902915bd5.png
test.png
987457-e5eec0f2cb1a7e0c.png
demo.png

相關文章