對(duì)于推送噪馏,不管是我們開發(fā)人員還是用戶祟滴,都是又愛又恨振惰,特別是iOS的推送蘋果爸爸幾乎每出一個(gè)版本的iOS系統(tǒng)都要給他一兩斧子,以改善用戶體驗(yàn)和安全性垄懂,因?yàn)闁|西實(shí)在又多又雜骑晶,所以我只說說我們常用到的一些東西。
推送無外乎就兩個(gè)草慧,一個(gè)是本地推送桶蛔,一個(gè)是遠(yuǎn)程推送,但是二者其實(shí)是相互依存的漫谷。
我們做出來的時(shí)候仔雷,還要特別注意第三個(gè)東西,那就是APP的狀態(tài):
UIApplicationStateActive,// 激活狀態(tài)舔示,用戶正在使用
AppUIApplicationStateInactive,// 待激活狀態(tài)碟婆,即用戶切換到其他App、按Home鍵回到桌面惕稻、拉下通知中心
UIApplicationStateBackground// 在后臺(tái)運(yùn)行
其實(shí)竖共,還有一個(gè)更棘手的狀態(tài),那就是app被殺死的狀態(tài)俺祠,文章末尾我將說到公给。
本地推送一般和APP的UIApplicationStateBackground這個(gè)狀態(tài)密切相關(guān),其他狀態(tài)要么沒有多大必要蜘渣,要么接收不到(殺手狀態(tài))淌铐,在收到遠(yuǎn)程推送后,先判斷狀態(tài):
switch (state) {
???? case UIApplicationStateActive:{}//可以播放聲音震動(dòng)提示
??? break;
? ? case UIApplicationStateInactive:{}//可以播放聲音震動(dòng)提示
?? break;
?? case UIApplicationStateBackground:{
? ? ? ? [self showNotificationLocalNotification];//生成本地推送
?? }
?? break;
?? default:
?? break;
}
本地推送到了iOS10蔫缸,有很大的改動(dòng)腿准,所以要判斷是否是iOS10:
- (void)showNotificationWithMessage:(EMMessage *)message
{
//接連推送判斷
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];
BOOL playSound = NO;
if (!self.lastPlaySoundDate || timeInterval >= kDefaultPlaySoundInterval) {
self.lastPlaySoundDate = [NSDate date];
playSound = YES;
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:@"xxx" forKey:@"key"];
if (IOS10) {
//發(fā)送本地推送
if (NSClassFromString(@"UNUserNotificationCenter")) {
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
if (playSound) {
content.sound = [UNNotificationSound defaultSound];
}
content.body =alertBody;
content.userInfo = userInfo;
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.messageId content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
}
else {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date]; //觸發(fā)通知的時(shí)間
NSString *title = message.ext[kChatUserNick];
}
}
notification.alertBody =[NSString stringWithFormat:@"%@:%@", title, messageStr];;
notification.alertAction = NSLocalizedString(@"open", @"Open");
notification.timeZone = [NSTimeZone defaultTimeZone];
if (playSound) {
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.userInfo = userInfo;
//發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
UIApplication *application = [UIApplication sharedApplication];
application.applicationIconBadgeNumber += 1;
}? else {//iOS10 以下
//發(fā)送本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date]; //觸發(fā)通知的時(shí)間
notification.alertBody = @"xxxx";
notification.alertAction = @"打開";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.userInfo = userInfo;
//發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
UIApplication *application = [UIApplication sharedApplication];
application.applicationIconBadgeNumber += 1;
}
}
以上是接到遠(yuǎn)程推送并發(fā)送本地推送的代碼,很多童鞋會(huì)認(rèn)為這是自定義推送捂龄,其實(shí)也可以這么理解释涛,那么怎么接收到本地推送呢加叁?
很簡單,在AppDelegate里實(shí)現(xiàn)這個(gè)方法就OK:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
這個(gè)方法中你要放你接收到本地推送唇撬,用戶點(diǎn)擊后要處理的事情它匕,比如跳轉(zhuǎn)到某個(gè)指定頁面,那么問題又來了窖认,怎么跳轉(zhuǎn)到指定頁面呢豫柬?
這個(gè)問題就和你APP的架構(gòu)密切相關(guān)了,我只說說我的思路
1.找到APP此時(shí)頂層的UIViewController
2.回到rootViewControlle
3.跳轉(zhuǎn)至頁面
當(dāng)然扑浸,在APP殺死的狀態(tài)下烧给,你將只能收到遠(yuǎn)程推送,如果用戶點(diǎn)擊了通知喝噪,將進(jìn)入下面的方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
注意础嫡,遠(yuǎn)程推送有一定的延遲。
最后說一個(gè)問題酝惧,希望有大神來指點(diǎn)迷津:
APP在綁定了推送之后榴鼎,被用戶卸載了,然后又重裝了晚唇,也能收到推送巫财,怎么破?哩陕?平项??目前發(fā)現(xiàn)微信也存在這個(gè)問題悍及,真心求解C銎啊!P母稀鸳粉!