极光推送提供三种方法实现Notification通知

  1. 三方开发平台发送普通消息,客户端设置PushNotificationBuilder,实现基础的Notification通知
  2. 三方开放平台发送普通消息,客户端设置CustomPushNotificationBuilder,实现高级自定义的Notification通知
  3. 三方开放平台发送自定义消息,客户端默认不处理此类消息,客户端定义Receiver接收自定义消息并进行处理
  4. 使用普通内容为空,将普通消息转成自定义消息类型,进而达到所需效果

但是前三种方案无法实现自定义声音,只有第四种方案可实现自定义Notification并进行通知

尝试方案一:

思路:接收三方开放平台自定义消息,自定义Notification更改提示音

AndroidManifest.xml<receiver
    android:name=".application.MyReceiver"android:enabled="true"><intent-filter ><!-- Required 用户注册SDK的intent --><action android:name="cn.jpush.android.intent.REGISTRATION" /><!-- Required 用户接收SDK消息的intent --><action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /><!-- Required 用户接收SDK通知栏信息的intent --><action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /><!-- Required 用户打开自定义通知栏的intent --><action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /><!-- Optional 用户接受Rich Push Javascript 回调函数的intent --><action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /><!-- 接收网络变化 连接/断开 since 1.6.3 --><action android:name="cn.jpush.android.intent.CONNECTION" /><category android:name="应用包名" /></intent-filter>
</receiver>
MyReceiver.javapublic class MyReceiver extends BroadcastReceiver {private static final String TAG = MyReceiver.class.getSimpleName();private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3;   //推送通知最多显示条数@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle =intent.getExtras();//if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)){Log.i(TAG, "接收到了通知");String title=bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);String content=bundle.getString(JPushInterface.EXTRA_ALERT);String extra=bundle.getString(JPushInterface.EXTRA_EXTRA);Log.i(TAG, "标题:【"+title+"】,内容:【"+content+"】,附加参数:【"+extra+"】");}else if(intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){Log.i(TAG, "接收到了消息");String message =bundle.getString(JPushInterface.EXTRA_MESSAGE);processCustomMessage(context, bundle);Log.i(TAG, "接收到的消息是:【"+message+"】");}else if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)){Log.i(TAG, "用户正在打开通知");}}/*** 实现自定义推送声音* @param context* @param bundle*/private void processCustomMessage(Context context, Bundle bundle) {NotificationCompat.Builder notification = new NotificationCompat.Builder(context);String title = bundle.getString(JPushInterface.EXTRA_TITLE);String msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_mdpi);Intent mIntent = new Intent(context,****.class);mIntent.putExtras(bundle);mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);notification.setContentIntent(pendingIntent).setAutoCancel(true).setContentText(msg).setContentTitle(title.equals("") ? "title": title).setSmallIcon(R.mipmap.icon_mdpi).setLargeIcon(bitmap).setNumber(NOTIFICATION_SHOW_SHOW_AT_MOST);Log.e(TAG, "processCustomMessage: extras----->" + extras);if (!TextUtils.isEmpty(extras)) {try {JSONObject extraJson = new JSONObject(extras);if (null != extraJson && extraJson.length() > 0) {String sound = extraJson.getString("sound");if("1".equals(sound)){notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.default_push_sound));} else {notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));}}} catch (JSONException e) {e.printStackTrace();}}NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);notificationManager.notify(NOTIFICATION_SHOW_SHOW_AT_MOST, notification.build());  //id随意,正好使用定义的常量做id,0除外,0为默认的Notification}
}

效果实现了,跟服务端沟通,发现只有开发平台上有自定义消息,服务端没自定义消息的api接口,烦唷!!!

尝试方案二:

思路:查看接口文档,发现极光推送采用Receiver接收普通消息和自定义消息,通过拦截广播,不让极光三方库处理默认的Notification

查看极光AndroidManifst.xml的PushReceiver配置

<!-- Required SDK核心功能 -->
<receiver
    android:name="cn.jpush.android.service.PushReceiver"android:enabled="true"><intent-filter android:priority="1000"><action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /><!-- Required  显示通知栏 --><category android:name="应用包名" /></intent-filter><intent-filter><action android:name="android.intent.action.USER_PRESENT" /><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter><!-- Optional --><intent-filter><action android:name="android.intent.action.PACKAGE_ADDED" /><action android:name="android.intent.action.PACKAGE_REMOVED" /><data android:scheme="应用包名" /></intent-filter>
</receiver>

发现极光推送通过PushReceiver来代理接收派发普通消息和自定义消息。那我就利用MyReceiver,而且优先级为65535,保证最先接收到消息

在MyReceiver中普通消息过滤中添加abortBroadcast(); 吸收广播,禁止往下传递广播,运行后提示“BroadcastReceiver trying to return result during a non-ordered broadcast”,居然是无序广播。。。 这。。。。极光你就不能给个活路么

尝试方案三:

思路:不让我自定义,那我就把默认的铃声去掉,然后在接收到普通消息广播之后使用SoundPool播放提示音乐

init JPush之后
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
builder.statusBarDrawable = R.drawable.icon_mdpi;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
JPushInterface.setPushNotificationBuilder(1, builder);

notificationDefaults 是通过设置二进制位来判断的,DEFAULT_SOUND=1

使用红米Note4做测试,还是会有提示音。。 不想做了。。。

尝试方案四:

思路:查看文档时无意间发现,当普通通知内容为空,将不执行默认的Notification,使用extra传递Notification.title和Notification.msg,再接收到普通消息之后执行之前实现的自定义processCustomMessage方法

接受广播如果全部类型的广播都接收,则需要在 AndroidManifest.xml 里添加如下的配置信息:
<receiver
    android:name="Your Receiver"android:enabled="true"><intent-filter><action android:name="cn.jpush.android.intent.REGISTRATION" /><action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" /><action android:name="cn.jpush.android.intent.CONNECTION" /><category android:name="You package Name" /></intent-filter>
</receiver>
Action - JPushInterface.ACTION_NOTIFICATION_RECEIVED.字符串值 "cn.jpush.android.intent.NOTIFICATION_RECEIVED"功能描述:
收到了通知 Push。
如果通知的内容为空,则在通知栏上不会展示通知。
但是,这个广播 Intent 还是会有。开发者可以取到通知内容外的其他信息。

终于搞定了,再也不想用极光,好麻烦-。-

极光推送JPush---自定义提示音相关推荐

  1. android极光推送声音,Android 极光推送JPush---自定义提示音

    public classMyReceiver extends BroadcastReceiver {private static final String TAG = MyReceiver.class ...

  2. ionic4 集成极光推送jpush

    ionic4 集成极光推送jpush 1. 在极光官网注册.登录.创建应用 极光推送官网 应用包名要与config.xml一致 2.安装插件 ionic cordova plugin add jpus ...

  3. 极光推送 JPush 简介 集成

    JPush产品简介 控制台: https://www.jiguang.cn/dev/#/app/list#dev 极光推送是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发 ...

  4. 极光推送JPush的快速集成

    首先到极光推送的官网上创建一个应用,填写对应的应用名和包名. 创建好之后下载Demo 提取Sdk里面的图片和xml等资源文件放自己项目的相应位置,然后要注意的是.so文件的放置位置: 在main目录下 ...

  5. 关于Cordova iOS 工程极光推送通知自定义声音的设置

    前提条件:你已经在极光官网后台申请了极光推送所需要的APP_KEY,以及在苹果开发者中心在你的项目中开启了推送权限,并制作了推送证书上传到极光后台,前面这些不知道怎么设置的,请自行问度娘.没办法,io ...

  6. 极光推送jpush(简单易懂,分分钟教你搞定)

    先注册账户: 然后点击开发者服务:点击打开链接 创建应用: 随便起个名字,但是最好和你的应用名字一样 然后点击下一步推送设置 把你的工程应用名字输入: 应用包名就是build.gradle文件里的ap ...

  7. iOS开发之极光推送JPush

    JPush介绍 JPush可以轻松地通过极光推送完成运营推送工作同时支持自定义消息.富媒体消息.应用内提醒消息.短视频消息和围栏消息等9种类型,满足开发者在各类应用场景下运营所需9种消息类型. 配置i ...

  8. 极光推送后台php接口,极光推送Jpush(v2)接口 服务端PHP版本的REST API推送类

    在许多的手机App开发中推送是一个必须的应用.高大上的企业都会自己投入成本搭建自己的推送服务器,而小微企业则会选择一些服务商,使用他们的服务,减轻自己的运营和维护的成本.Jpush(极光推送)是目前比 ...

  9. 第一篇博客:极光推送以及自定义推送声音

    极光注册就不说了,首先配置App的build.grandle 在defaultConfig里粘贴一下内容appkey为我在极光注册之后的Appkey 然后配置AndroidManifest  把下面这 ...

最新文章

  1. 2018-04-07进程创建学习流程
  2. git revert改写提交
  3. php8vsgo,vscode编辑好go语言代码要怎么运行
  4. 信息学奥赛一本通(1232:Crossing River)
  5. python 模拟io_Python 的五种io模型理解
  6. CLR via C#学习笔记-第十章-无参属性
  7. Vivado封装自定义IP
  8. weblogic 12c 安装与下载
  9. 电信吉比特光纤猫虚拟服务器设置,光猫设置教程 华为光猫设置教程
  10. ai图片怎么把图中的字改掉_如何修改图片上的文字
  11. qq邮箱服务器连接错误代码,qq邮箱错误代码103打不开是怎么回事
  12. 什么是回归问题和分类问题?机器学习知识点
  13. 将Excel中的数据导入html以及将html表格数据导出Excel
  14. 租一个月的云服务器要花费多少?
  15. MySQL查询某个列中相同值的数量统计
  16. ChinaRAP中国道路(路网)风险评估系统研究与应用
  17. 思否网站随屏幕大小自动发生变化
  18. 17、内网渗透测试定位技术总结
  19. Automatic Portrait Segmentation for Image Stylization 翻译学习
  20. 牛客 HJ32 密码截取

热门文章

  1. android基础--短信库解析
  2. L2 Cache——CPU二级缓存
  3. Python笔记_第一篇_童子功_0.开场白
  4. 清除浏览器缓存的所有方法
  5. 某供水开票系统之渗不透测试
  6. python-shutil模块
  7. 疯狂边缘的野兽:新浪微博繁华下的危机
  8. oracle 外键更改为on delete cascade属性,SQL脚本更改所有外键以添加ON DELETE CASCADE
  9. javascript通过点击事件启动QQ程序
  10. apache大师+伪静态_Apache配置伪静态