- 可以讓不在前臺運行的APP, 告知用戶APP內部發送了什麼事情; 簡單來說就是程序不在線或程序在後臺想要接收消息, 那麼就要用到推送通知來實現 ;
效果:
- 在屏幕頂部顯示一塊橫幅(QQ)
- 在屏幕中間顯示一個alertview(鬧鐘, 本地推送, 不需要網絡)
- 鎖屏時也照樣呈現
- 呈現推送通知的同時, 還可以更新APP圖標數字
- 用戶接受的推送通知, 都會展示在通知中心
本地推送Local Notification
來自本地通知頭文件:
In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to schedule and present UILocalNotifications
在IOS8.0及以後, 你的應用在定製和展示通知之前必須註冊用戶通知.
由此: 本地通知分爲4個步驟-創建, 註冊, 定製, 展示
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//0.註冊
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:setting];
//1.創建一個通知
UILocalNotification *local = [[UILocalNotification alloc]init];
//初始化 5秒
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 提示主題
local.alertBody = @"女神:在嗎?";
// 圖標數字
local.applicationIconBadgeNumber = 11;
local.soundName = UILocalNotificationDefaultSoundName;
// 是否顯示 滑動按鈕
local.hasAction = YES;
// 滑動來解鎖的按鈕文字
local.alertAction = @"聊天";
// 攜帶參數
local.userInfo = @{@"name":@"女神" , @"info":@"在嗎?" , @"qq": @"110"};
//2.定製 //3.展示
[[UIApplication sharedApplication]scheduleLocalNotification:local];
}
現在運行程序我們可以看到:
//1.定製 之後程序在前臺 沒顯示橫幅 圖標文字 改了 直接接收通知
//2.程序在後臺 顯示橫幅 有通知
//3.程序死了 顯示橫幅 有通知
//4.點擊了橫幅 程序啓動
//5.不點橫幅 不發生事情
那麼接下來我們驗證:
//1.程序活着 或者 程序 死了 是否能接收通知
//2.點擊橫幅 是否是接收通知(內容)
**處理程序在前臺或在後臺的跳轉**
// 接收到通知會調用這個方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// 程序死了, 不會在控制檯打印, 想驗證是否接收到了通知, 採用添加控件的調試方法
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
lable.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:lable];
// 對於死了的程序這麼驗證不夠嚴謹, 因爲View可能不存在啦
// 因此我們考慮採用寫入本地文件或者更改圖標文字
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
// 數據通過userInfo攜帶過來
NSLog(@"%@",notification.userInfo);
//跳轉聊天界面
[self jumpToChatWith:notification.userInfo];
}
總結:
//死了:
點擊橫幅沒有來到這個方法
//活着
//1.前臺: 默認直接接收(沒有橫幅) 來到這個方法
//2.後臺: 點擊恆幅就會接收 會來到這個方法
**處理程序從死到生: 點擊圖標/接收通知 的界面跳轉**
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//更改圖標文字
//[UIApplication sharedApplication].applicationIconBadgeNumber = 22;
//接收通知 還是 點擊圖標?
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
lable.backgroundColor = [UIColor redColor];
lable.numberOfLines = 0;
//顯示通知
[self.window.rootViewController.view addSubview:lable];
if (launchOptions == nil) { //點擊圖標
}else if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]){ //接收通知
//跳轉界面
UILocalNotification *local = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
lable.text = [NSString stringWithFormat:@"%@",local.userInfo];
[self jumpToChatWith:local.userInfo];
}else{ //其他
}
return YES;
}
界面跳轉
//接收到通知之後跳轉界面
- (void)jumpToChatWith:(NSDictionary *)userInfo
{
CZChatViewController *chat = [[UIStoryboard storyboardWithName:@"chat" bundle:nil] instantiateInitialViewController];
NSLog(@"%@",self.window.rootViewController);
self.window.rootViewController = chat;
}