美文网首页推送Android开发
极光推送自定义notification sound + Soun

极光推送自定义notification sound + Soun

作者: darkengine | 来源:发表于2017-10-27 19:04 被阅读53次

从需求说起吧,产品经理/老板说有客户想要有推送通知到达的时候有声音,为了凸显公司的特色我们自己定义个通知声加进去吧。就这么个需求iOS实现起来还是挺简单的,根据文档拖一个符合要求的格式(不要超过30秒)例如叫notif.caf放到项目根目录,极光推送的时候把sound字段设置为notif.caf就行了。
Android的话有几种办法,一个是完全自定义notification的样式,包括ui+声音。不过我们一直用默认的用得好好的就没必要折腾了,选择的方案是ui仍然用原生的,而且后台发推送的时候给android发送的是无声的,我们在onReceive()里面自己播放自定义声音。
于是一查SoundPool就开干了,看api还是很简单的:新建个sound pool,load然后加载,代码自然就出来了:

SoundPool.Builder builder = new SoundPool.Builder();
builder.setMaxStreams(1);
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_ALARM);
builder.setAudioAttributes(attrBuilder.build());
SoundPool soundPool = builder.build();

新建一个,为了保证能播放成功最好在load成功的回调里边播放,结果as的自动补全给了一段这样的代码:

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int i, int i1) {

    }
});

嗯?i, i1什么玩意儿(论参数命名的重要性……),肯定是状态什么的不管了。然后在里边play,play函数的签名如下:

play(int, float, float, int, int, float))(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

于是就这么掉坑里了:

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int i, int i1) {
        soundPool.play(R.raw.bird, 0.99f, 0.99f, 0, 0, 1);
    }
});

soundID嘛,那个res id也是id,放进来非常合理,于是编译-通过,运行-通过,测试 - 没声音。。。换了几个android版本的测试机还是这样,只能狗哥家搜了下发现是这么弄的:

SoundPool.Builder builder = new SoundPool.Builder();
builder.setMaxStreams(1);
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_ALARM);
builder.setAudioAttributes(attrBuilder.build());
SoundPool soundPool = builder.build();
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        if (status == 0) {
            soundPool.play(sampleId, 0.99f, 0.99f, 0, 0, 1);
        }
    }
});
soundPool.load(context, R.raw.bird, 0);

那两个as自作聪明给我补全的参数原名是sampleId和status,呵呵呵呵。
还好只浪费了半个小时……

相关文章

  • 极光推送自定义notification sound + Soun

    从需求说起吧,产品经理/老板说有客户想要有推送通知到达的时候有声音,为了凸显公司的特色我们自己定义个通知声加进去吧...

  • Gentle sound

    And suddenly there was another sound - a very gentle soun...

  • iOS 远程推送APNS从0至发布-证书集成篇

    简介 APNS全称Apple Push Notification service 说明 此文以极光推送来举例,相信...

  • iOS 极光推送了解

    极光推送:JPush iOS推送分为两种:第一种是APNs[(Apple Push Notification Se...

  • iOS集成极光推送

    iOS集成极光推送 一、配置APNs 苹果APNs(英文全称:Apple Push Notification Se...

  • 极光推送的几个问题

    极光推送的几个问题 最近接入极光推送遇到了几个问题 服务端发出通知没有声音 需要设置声音字段的值, sound:'...

  • 在 android Notification使用PendingI

    项目的推送是采用极光推送,使用的是极光推送自定义消息,自己弹出通知栏,当有多天消息推送的时候PendingI...

  • 极光推送第二篇:消息接收

    极光推送第一篇:配置极光推送第三篇:消息跳转和自定义 上一篇极光推送第一篇:配置中的第三节我们说到了需要通过自定义...

  • 极光推送第一篇:配置

    极光推送第二篇:消息处理极光推送第三篇:消息跳转和自定义 相信很多人都是用的极光推送,因为名字好看。官方也有文档:...

  • 005. 发布app项目和更新app版本

    一. 发布,更新过程: 0. 发送通知,不是自定义消息; 打开极光推送官网,登录,通过极光推送,发送通知; 通知:...

网友评论

  • e7fa46c17820:正在做这个功能,请教一下,当APP后台运行的时候收到推送,音乐播放不完整,只能播两三秒,这个问题大佬遇到过么。知道怎么解决么:broken_heart:
    darkengine:检查下是不是极光推送本身的通知声音没有关闭,结果极光的提示音把你自定义的音乐给中断了
  • darkengine:再来补充一波,Android 7.0开始,官方设置支持单独关闭某个app的通知,所以在播放的时候需要先判断自己的app通知有没有被关掉,有的话就不要播放了(我们用的极光推送,其实这个应该在sdk里面就有判断的,通知关闭了就不要回调receiver里边的函数才对)。

    if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) {
    return;
    }
  • darkengine:另外补充一下,如果像我一样是用来做自定义notification铃声的,attrBuilder.setLegacyStreamType(AudioManager.STREAM_ALARM);改成AudioManager.STREAM_NOTIFICATION才对,不然手机开成震动了还是会响。
  • 秦子帅:厉害了,可否关注一下我的微信公众号:安卓干货铺。 一起交流

本文标题:极光推送自定义notification sound + Soun

本文链接:https://www.haomeiwen.com/subject/esedpxtx.html