android自定义push通知_GitHub - legendyumi/MyJpush: Android极光8.0自定义通知声音

本文介绍了如何在Android8.0及以上版本使用极光推送自定义通知声音。步骤包括升级极光SDK至3.3.4,配置Gradle文件,创建NotificationChannel,放置MP3文件,以及在自定义广播接收器中处理通知,确保ChannelId的唯一性以避免声音失效。
摘要由CSDN通过智能技术生成

Android极光8.0自定义通知声音

Android8.0以来通知确实用了NotificationChannel,项目中另一处的通知在8.0以上的手机有自定义的通知音,但是极光收到的推送则一直为默认的提示音。后来发现极光还未支持。从3.3.4才开始支持NotificationChannel。

Android极光8.0自定义通知声音:

1.升级极光sdk到3.3.4,并配置;

注意:

1.项目的gradle文件里:

//极光推送

implementation 'cn.jiguang.sdk:jpush:3.3.6'

implementation 'cn.jiguang.sdk:jcore:2.1.6'

manifestPlaceholders = [

JPUSH_PKGNAME: applicationId,

JPUSH_APPKEY : "", //JPush上注册的包名对应的appkey.

JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.

]

2.manifest文件:

android:process=":pushcore">

android:name=".jpush.MyReceiver"

android:enabled="true"

android:exported="false">

3.Application注册极光:

JPushInterface.setDebugMode(false);

JPushInterface.init(getApplicationContext());

2.在res文件夹下新建raw目录,放入MP3文件(提示音);

3.自定义广播接收器,在接收到通知时自定义声音:

注意:ChannelId保持唯一,跟手机里其他APP的channelid一样时,声音会失效

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

//注意id,保持唯一,跟手机里其他APP的channelid一样时,声音会失效

NotificationChannel channel = new NotificationChannel("TEST", "自定义通知", NotificationManager.IMPORTANCE_HIGH);

String alert = bundle.getString(JPushInterface.EXTRA_ALERT);

String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

Log.e("返回信息", extras);

if (alert != null && !alert.equals("")) {

String type = "";

try {

JSONObject extrasJson = new JSONObject(extras);

if (extrasJson.has("type")) {

type = extrasJson.getString("type");

}

} catch (Exception e) {

Log.w(TAG, "Unexpected: extras is not a valid json", e);

return;

}

if (type.equals("0") || type.equals("1") || type.equals("3") || type.equals("4") || type.equals("7")) {

channel.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.notice),

new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)

.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)

.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT).build());

}

}

manager.createNotificationChannel(channel);

Notification notification = new NotificationCompat.Builder(context, "TEST")

.setOnlyAlertOnce(true)

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setAutoCancel(true)

.build();

manager.notify(1, notification);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值