EBForeNotification 1.0.8

EBForeNotification 1.0.8

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最后发布2017年10月

pikacodepikacode维护。



  • E.B

寻找 英文 README.md

QQ: 57380422

EBForeNotification

当 App 处于前台时展示与系统完全一样的推送弹窗声音。获取推送内容,并且处理点击事件。

支持 iOS 7~10 beta,支持模拟器真机运行。

新增

  • 下拉手势
  • iOS 10 弹窗样式,调用方法 (iOS 10 样式暂不支持下滑手势多行内容)

    [EBForeNotification handleRemoteNotification:userInfo soundID:soundID isIos10:YES];
    }
    
    [EBForeNotification handleRemoteNotification:userInfo customSound:soundName isIos10:YES];

效果

  • 与系统推送弹窗 UI 效果完全相同
  • 可以自动获取 App 的应用名称应用图标
  • 弹窗时会自动隐藏系统状态栏、收起后自动显示系统状态栏
  • 自带推送声音
  • 时间及下方收拉条的颜色与当前页面的背景颜色相同
  • 自带点击事件,点击可获取推送内容,进行相应页面跳转
  • 自带上滑手势,快速收起
  • 自带下滑手势,展开消息完整内容
  • 自动在最前端的 controller 上进行弹窗

实际效果如下:

  • iOS 10 以前样式

  • iOS 10 样式

安装

Pod 安装

platform :ios, '7.0'

target 'YourTargetName' do
    pod 'EBForeNotification'
end

手动 安装

下载并在 Xcode 中将根目录中的 EBForeNotification 文件夹拖拽拷贝到 Xcode 工程。

本地弹窗

您可以在任何方法中调用以下任一行代码来弹窗:

#import "EBForeNotification.h"
{...
//普通弹窗(系统声音)
[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"];
...}

接收远程/本地推送后弹窗

接收远程/本地推送后,会自动在前台展示推送弹窗及声音。

AppDelegate.m 中添加以下代码:

//AppDelegate.m
#import "EBForeNotification.h"

//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 预定义声音 备用地址 AudioServices 声音

监听并处理点击事件

添加 Observer 来监听 EBBannerViewDidClick,获取推送内容,通过推送时自定义的字段处理自己的逻辑,例如:跳转到对应页面等。

接收到的推送内容类似以下:

{
    "aps":
    {
        "alert":"推送内容",
        "sound":"sound",
        "badge":"3"
    },
        "key1":"跳转页面1"  //自定义此字段以跳转到相应页面
}

添加 Observer 来获取自定义字段并进行处理:

#import "EBForeNotification.h"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eBBannerViewDidClick:) name:EBBannerViewDidClick object:nil];
-(void)eBBannerViewDidClick:(NSNotification*)noti{
    if(noti[@"key1" == @"跳转页面1"]){
        //跳转到页面1
    }
}

Demo

下载并运行 EBForeNotification demo