具体代码:参看以下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 } }