极光推送提供三种方法实现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. -->
<action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="应用包名" />
</intent-filter>
</receiver>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
private static final String TAG = MyReceiver.class.getSimpleName();
private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = ; //推送通知最多显示条数 @Override
public 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, , mIntent, ); 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() > ) {
String sound = extraJson.getString("sound");
if("".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="">
<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(, builder);
notificationDefaults 是通过设置二进制位来判断的,DEFAULT_SOUND=

使用红米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 还是会有。开发者可以取到通知内容外的其他信息。
终于搞定了,再也不想用极光,好麻烦-。-

Android 极光推送JPush---自定义提示音的更多相关文章

  1. 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

    Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...

  2. 关于极光推送Jpush的demo

    关于极光推送Jpush 推送是手机app必不可少的一样功能,这次由于公司项目需要研究了一下.由于推送一般写于服务端,所以对于不会Android的javaweb程序员要写出一个完整的demo是一件很头痛 ...

  3. Android 极光推送造成IM服务绑定失败bug

    由于极光推送对8.0的支持问题,升级到了最新版本的极光推送.但是最新版本的极光推送,默认将推送服务设置到了新的进程里面,由此引发 Android 极光推送多进程造成的application运行两次 和 ...

  4. 李洪强iOS开发之极光推送JPush

    李洪强iOS开发之极光推送JPush

  5. Android消息推送——JPush极光推送

    刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...

  6. 极光推送 JPush 简介 集成 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

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

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

  8. 极光推送Jpush(v3)服务端PHP版本的api脚本类

    原文地址:http://www.dodobook.net/php/780 关于极光推送的上一篇文章已经说明了,此处就不多说了.使用v3版本的原因是v2使用到2014年年底就停止了.点击查看上一篇的地址 ...

  9. 极光推送Jpush(v3)服务端PHP版本集成(V3版本只调用推送API)

    因为版本升级,极光推送的API也有了V3,功能也更丰富了,但是对于我们有的用户来说,我们还是只需要调用推送的API就够了. 下载了一份PHP服务端的SDK(下载地址:http://docs.jpush ...

随机推荐

  1. 【NOIP 2009】最优贸易

    描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通 ...

  2. PAT天梯赛L1-020 帅到没朋友

    题目链接:点击打开链接 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(<=100),是已知朋 ...

  3. POJ1053 Set Me

    题目来源:http://poj.org/problem?id=1053 题目大意: 有一种牌,共有81张.每张牌有四个属性,每种属性有三种可能取值:形状(D,O,S),数字(1,2,3),颜色(R,G ...

  4. Codeforce-A-Two distinct points(暴力)

    output standard output You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. ...

  5. P5021 赛道修建 (NOIP2018)

    传送门 考场上把暴力都打满了,结果文件输入输出写错了.... 当时时间很充裕,如果认真想想正解是可以想出来的.. 问你 长度最小的赛道长度的最大值 显然二分答案 考虑如何判断是否可行 显然对于一个节点 ...

  6. Codeforces 527C Glass Carving (最长连续0变形+线段树)

    Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glas ...

  7. 在x64平台上调试依赖于x86的WCF服务

    根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的安全.可信赖.事务性 ...

  8. WindowsMTU修改

    MTU是英文Maximum Transmission Unit的缩写,意为"最大传输单位".也就是通过TCP/IP协议所传输的数据包最大有多少字节,对于网速有极大的影响, MTU并 ...

  9. vue 之 nextTick 与$nextTick

    VUE中Vue.nextTick()和this.$nextTick()怎么使用? 官方文档是这样解释的: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 D ...

  10. 原生JS实现图片拖拽移动与缩放

    看一下最终效果,图片可以拖动,可以缩放 把代码贴出来,可以直接粘贴使用,大致的思想就是鼠标按下的时候获取当时的鼠标位置,要减去left和top值,移动的时候获取位置减去初始的值就得到移动的时候的lef ...