iOS8本地推送
- 在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;
}
}
}
}
示例圖:

test.png

demo.png
相關文章
- iOS 本地推送iOS
- 將本地nuget包推送到Nexus
- ios8系統定位問題iOS
- Git 本地推送遠端失敗 non-fast-forwardGitASTForward
- Git回滾本地已提交未推送的程式碼Git
- 本地倉庫推送到遠端倉庫的git操作Git
- Maven推送本地jar包到遠端私有倉庫配置MavenJAR
- iOS8 Framework使用載入xib及圖片iOSFramework
- 推送服務本地通知頻次及分類管控通知
- git本地分支對映遠端分支並推送相應程式碼Git
- git本地分支與遠端分支名稱不相同,推送到遠端Git
- 【FAQ】HMS Core推送服務與本地建立通知訊息如何相互覆蓋?
- iOS8 自定義UITabBar (使用popToViewController導致的UITabBarButton重疊的問題)iOSUItabBarViewController
- iOS 推送通知及推送擴充套件iOS套件
- wx推送
- 大批次推送郵件,請教如何提升推送速率
- APP訊息推送 極光推送 示例程式碼APP
- ReactNative版友盟推送React
- gitlab 推送程式碼Gitlab
- PHP與反ajax推送,實現的訊息實時推送功能PHP
- fluentd 推送 mariadb audit log
- MobPush推送實現解析
- Prometheus&Alertmanager告警推送Prometheus
- 微信每日早安推送
- React Native 跳轉到 APP 推送頁面並獲取推送狀態React NativeAPP
- 極光推送申請iOS推送證書p12及配置流程iOS
- 極光推送申請iOS推送證書p12及配置教程iOS
- maven 將本地jar打入本地倉庫MavenJAR
- 外貿郵件推送平臺,外貿開發信郵件推送平臺,外貿EDM推送平臺,如何選擇?
- Remix本地化,載入本地合約檔案,本地連結RemixREM
- Android之訊息推送原理Android
- workerman 實現訊息推送
- iOS 推送(蘋果原生態)iOS蘋果
- 推送MobPush-API說明API
- 關於推送uni-push
- docker 打包映象推送至dockerhubDocker
- 實時訊息推送整理
- 搞掂釘釘訊息推送!
- 如何實現定時推送?