在 iOS 中收到推送通知時(shí),如果 App 處于前臺(tái)運(yùn)行的情況下,推送的頂部彈窗是不會(huì)彈出來(lái)的。
然而就是有很多的產(chǎn)品經(jīng)理都會(huì)提出類似這樣的需求:
那就是在 App 處于前臺(tái)時(shí)一樣要彈出推送的窗口,而且還要能點(diǎn)擊,能跳轉(zhuǎn)到指定頁(yè)面,甚至這一需求還涉及到了產(chǎn)品的核心功能。
今天跟大家分享一下自己寫(xiě)的小插件,僅僅只需 1、2 行代碼,即可搞定這一需求。
Github: https://github.com/Yasashi/EBForeNotification
在 App 處于前臺(tái)時(shí)展示跟系統(tǒng)完全一樣的推送彈窗和聲音。獲取推送內(nèi)容,并且處理點(diǎn)擊事件。
支持 iOS 7~10 beta,支持模擬器和真機(jī)運(yùn)行。
- 跟系統(tǒng)推送彈窗 UI 效果完全相同
- 可以自動(dòng)獲取 App 的應(yīng)用名稱,應(yīng)用圖標(biāo)
- 彈窗時(shí)會(huì)自動(dòng)隱藏系統(tǒng)狀態(tài)欄、收起后自動(dòng)顯示系統(tǒng)狀態(tài)欄
- 自帶推送聲音
- 時(shí)間及下方收拉條的顏色跟當(dāng)前頁(yè)面的背景顏色相同
- 自帶點(diǎn)擊事件,點(diǎn)擊可獲取推送內(nèi)容,進(jìn)行相應(yīng)頁(yè)面跳轉(zhuǎn)
- 自帶上滑手勢(shì),快速收起
- 自動(dòng)在處于最前端的 controller 上進(jìn)行彈窗
安裝
下載并在 Xcode 中 拖拽拷貝 EBForeNotification 文件夾至 Xcode 工程。
targets --> Build Settings --> 搜 other link --> 添加 -ObjC。

本地彈窗
在任意方法內(nèi)調(diào)用以下任 1 行代碼即可彈窗
#import "EBForeNotification.h"
{...
//普通彈窗(系統(tǒng)聲音)
[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內(nèi)容"}} soundID:1312];
//普通彈窗(指定聲音文件)
[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內(nèi)容"}} customSound:@"my_sound.wav"];
//帶自定義參數(shù)的彈窗(系統(tǒng)聲音)
[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內(nèi)容"}, @"key1":@"value1", @"key2":@"value2"} soundID:1312];
//普通彈窗(指定聲音文件)
[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內(nèi)容"}, @"key1":@"value1", @"key2":@"value2"} customSound:@"my_sound.wav"];
...}
接收遠(yuǎn)程/本地推送后彈窗
接收遠(yuǎn)程/本地推送后,自動(dòng)在前臺(tái)展示推送彈窗及聲音。
在 AppDelegate.m 中添加代碼
//AppDelegate.m
#import "EBForeNotification.h"
//ios7 before
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
...
//系統(tǒng)聲音彈窗
[EBForeNotification handleRemoteNotification:userInfo soundID:1312];
//指定聲音文件彈窗
[EBForeNotification handleRemoteNotification:userInfo customSound:@"my_sound.wav"];
...
}
//ios7 later
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
...
//系統(tǒng)聲音彈窗
[EBForeNotification handleRemoteNotification:userInfo soundID:1312];
//指定聲音文件彈窗
[EBForeNotification handleRemoteNotification:userInfo customSound:@"my_sound.wav"];
...
completionHandler(UIBackgroundFetchResultNewData);
}
soundID 參數(shù)
iOS 系統(tǒng)自帶的聲音 id,系統(tǒng)級(jí)的推送服務(wù)默認(rèn)使用的是三全音,id = 1312
其他系統(tǒng)聲音 id 可以在這里查詢到http://iphonedevwiki.net/index.php/AudioServices#
備用地址:http://www.cocoachina.com/bbs/read.php?tid=134344
監(jiān)聽(tīng)并處理點(diǎn)擊事件
添加 Observer 監(jiān)聽(tīng) EBBannerViewDidClick,獲取推送內(nèi)容,通過(guò)推送時(shí)自定義的字段處理自己邏輯,如:跳轉(zhuǎn)到對(duì)應(yīng)頁(yè)面等。
接收到的推送內(nèi)容類似以下:
{
"aps":
{
"alert":"推送內(nèi)容",
"sound":"sound",
"badge":"3"
},
"key1":"跳轉(zhuǎn)頁(yè)面1" //自定義此字段以跳轉(zhuǎn)到相應(yīng)頁(yè)面
}
添加 Observer 獲取自定義的字段,并處理:
#import "EBForeNotification.h"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eBBannerViewDidClick:) name:EBBannerViewDidClick object:nil];
-(void)eBBannerViewDidClick:(NSNotification*)noti{
if(noti[@"key1" == @"跳轉(zhuǎn)頁(yè)面1"]){
//跳轉(zhuǎn)到頁(yè)面1
}
}