標籤:
具體代碼:參看以下demo
Github: https://github.com/Yasashi/EBForeNotification
具體操作如下:
1、將EBForeNotification檔案夾添加進入工程中
2、targets --> Build Settings --> 搜 other link --> 添加 -ObjC
3、本地彈窗
//普通彈窗(系統聲音)[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內容"}} soundID:1312];//普通彈窗(指定音效檔)[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內容"}} customSound:@"my_sound.wav"];//帶自訂參數的彈窗(系統聲音)[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內容"}, @"key1":@"value1", @"key2":@"value2"} soundID:1312];//普通彈窗(指定音效檔)[EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":@"展示內容"}, @"key1":@"value1", @"key2":@"value2"} customSound:@"my_sound.wav"];...}
4、接受遠程、本地推送彈窗
//ios7 before- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { ... //系統聲音彈窗 [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 { ... //系統聲音彈窗 [EBForeNotification handleRemoteNotification:userInfo soundID:1312]; //指定音效檔彈窗 [EBForeNotification handleRemoteNotification:userInfo customSound:@"my_sound.wav"]; ... completionHandler(UIBackgroundFetchResultNewData);}
註明:soundID 參數:iOS 系統內建的聲音 id,系統級的推送服務預設使用的是三全音,id = 1312,其他系統聲音 id 可以在這裡查詢到 iOS Predefined sounds,備用地址 AudioServices sounds
5、添加 Observer 監聽 EBBannerViewDidClick,擷取推送內容,通過推送時自訂的欄位處理自己邏輯,如:跳轉到對應頁面等。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eBBannerViewDidClick:) name:EBBannerViewDidClick object:nil];-(void)eBBannerViewDidClick:(NSNotification*)noti{ if(noti[@"key1" == @"跳轉頁面1"]){ //跳轉到頁面1 }}
iOS 前台時的推送彈窗效果