Android之极光推送之自定义声音踩坑记录(持续更新)


活动地址:CSDN21天学习挑战赛

一、极光推送之自定义声音踩坑记录

查看极光文档,极光配置理解是这样的,设置通道,就会覆盖本身的声音

参考文档

Android 极光推送JPush—自定义提示音
极光api文档
极光初步了解
极光推送(二)接收通知

极光自定义声音配置说明文档,ios可以直接通过字段值自定义声音,android不行
在这里插入图片描述

在res文件下新建raw目录存放指定声音

在这里插入图片描述

flutter两个build下配置安卓环境依赖

在这里插入图片描述

    implementation 'cn.jiguang.sdk:jpush:4.7.2'  // 此处以JPush 4.7.2 版本为例。
    implementation 'cn.jiguang.sdk:jcore:3.3.0'  // 此处以JCore 3.3.0 版本为例。

在这里插入图片描述
加入搜寻库

        jcenter()

xml

   <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="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="com.example.item_3" />
            </intent-filter>
        </receiver>

权限配置
在这里插入图片描述

    <uses-permission android:name="com.example.item_3.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在这里插入图片描述

配置原生channel

package com.example.item_3;
import static android.content.Context.NOTIFICATION_SERVICE;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = MyReceiver.class.getSimpleName();
    private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3; //推送通知最多显示条数
    private NotificationManager mManager;

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String contentMes = bundle.getString(JPushInterface.EXTRA_ALERT);
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            //上下文对象,ID,内容,标题,信道名称,级别
            createNotificationChannel(context, "1a0018970a5964ece1f", contentMes, title, "notification channel", NotificationManager.IMPORTANCE_HIGH);
            Log.i(TAG, "标题:【" + title + "】,内容:【" + contentMes + "】,附加参数:【" + extra + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
            Log.i(TAG, "接收到了消息");
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Log.i(TAG, "接收到的消息是:【" + message + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            Log.i(TAG, "用户正在打开通知");
        }
    }

    //上下文对象,ID,内容,标题,信道名称,级别
    public void createNotificationChannel(Context context, String channelID, String contentMes, String title, String channelNAME, int level) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //sdk版本大于26处理方法
            /**
             * 首先需要一个NotificationManager来进行管理,可以调用Context的getSystemService方法获取,这里传入一个Context。NOTIFICAATION_SERVICE即可
             */
            //创建通知管理实例
            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            channel.setLightColor(Color.GRAY);
            channel.enableLights(true);
            manager.createNotificationChannel(channel);
            //设置消息通知
            NotificationCompat.Builder notificationCmp = new NotificationCompat.Builder(context.getApplicationContext(), channelID);
            notificationCmp.setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)            //设置小图标
                    .setContentTitle(title) //设置标题
                    .setContentText(contentMes) //设置内容
                    .setVibrate(new long[]{0, 500, 1000})
                    .setDefaults(Notification.DEFAULT_LIGHTS);
//        调用notify()让通知显示出来(第一个参数是ID, 保证每个通知所指定的id都是不同的,第二个参数是notification对象)
            manager.notify(1, notificationCmp.build());
        }
    }
}

发现本地推送还是不能覆盖极光的声音

package com.example.item_3;
import static android.content.Context.NOTIFICATION_SERVICE;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = MyReceiver.class.getSimpleName();
    private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3; //推送通知最多显示条数
    private NotificationManager mManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
            //标题
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            //内容
            String contentMes = bundle.getString(JPushInterface.EXTRA_ALERT);
            //附加参数
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            //注册 id  1a0018970a5964ece1f
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "regId:" + regId);
            //
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            //上下文对象,ID,内容,标题,信道名称,级别
            createNotificationChannel(context, "1a0018970a5964ece1f", bundle, title, contentMes, extra);
            Log.i(TAG, "标题:【" + title + "】,内容:【" + contentMes + "】,附加参数:【" + extra + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
            Log.i(TAG, "接收到了消息");
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Log.i(TAG, "接收到的消息是:【" + message + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            Log.i(TAG, "用户正在打开通知");
        }
    }

    //上下文对象,ID,内容,标题,信道名称,级别
    public void createNotificationChannel(Context context, String channelID, Bundle bundle, String title, String contentMes, String extra) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            //设置消息通知
            NotificationCompat.Builder notification = new NotificationCompat.Builder(context.getApplicationContext(), channelID);
            notification.setAutoCancel(true)
                    .setContentText(title)
                    .setContentTitle(title)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.default_push_sound));
//        调用notify()让通知显示出来(第一个参数是ID, 保证每个通知所指定的id都是不同的,第二个参数是notification对象)
            manager.notify(bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), notification.build());
        }
    }
 }
package com.example.item_3;

import static android.content.Context.NOTIFICATION_SERVICE;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = MyReceiver.class.getSimpleName();
    private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3; //推送通知最多显示条数
    private NotificationManager mManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
            //标题
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            //内容
            String contentMes = bundle.getString(JPushInterface.EXTRA_ALERT);
            //附加参数
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            //注册 id  1a0018970a5964ece1f
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "regId:" + regId);
            //
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            //上下文对象,ID,内容,标题,信道名称,级别
            createNotificationChannel(context, "1a0018970a5964ece1f", bundle, title, contentMes, extra);
            Log.i(TAG, "标题:【" + title + "】,内容:【" + contentMes + "】,附加参数:【" + extra + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
            Log.i(TAG, "接收到了消息");
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Log.i(TAG, "接收到的消息是:【" + message + "】");
        } else if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            Log.i(TAG, "用户正在打开通知");
        }
    }

    //上下文对象,ID,内容,标题,信道名称,级别
    public void createNotificationChannel(Context context, String channelID, Bundle bundle, String title, String contentMes, String extra) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Log.i(TAG, "执行createNotificationChannel!!!!!!!!!");
            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            //创建通知渠道实例
            NotificationChannel channel  = new NotificationChannel(channelID, "supperman",NotificationManager.IMPORTANCE_HIGH);
            // 自定义声音
            channel.setSound(Uri.parse("android.resource://"  + context.getPackageName() + "/" + R.raw.default_push_sound), Notification.AUDIO_ATTRIBUTES_DEFAULT);//设置通知声音
            //创建和管理通知渠道
            manager.createNotificationChannel(channel);

            //设置消息通知
            NotificationCompat.Builder notification = new NotificationCompat.Builder(context.getApplicationContext(), channelID);
            Log.i(TAG, "执行notification!!!!!!!!!");
            notification.setAutoCancel(true)
                    .setContentText(title)
                    .setContentTitle(title)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.default_push_sound));
//        调用notify()让通知显示出来(第一个参数是ID, 保证每个通知所指定的id都是不同的,第二个参数是notification对象)
            playSound(context,R.raw.default_push_sound);
            Log.i(TAG, "执行notify!!!!!!!!!");
            manager.notify(1, notification.build());
        }
    }

    /**
     * 适合播放声音短,文件小
     * 可以同时播放多种音频
     * 消耗资源较小
     */
    public static void playSound(Context context, int rawId) {
        SoundPool soundPool;
        if (Build.VERSION.SDK_INT >= 21) {
            SoundPool.Builder builder = new SoundPool.Builder();
            //传入音频的数量
            builder.setMaxStreams(1);
            //AudioAttributes是一个封装音频各种属性的类
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            //设置音频流的合适属性
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
            builder.setAudioAttributes(attrBuilder.build());
            soundPool = builder.build();
        } else {
            //第一个参数是可以支持的声音数量,第二个是声音类型,第三个是声音品质
            soundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
        }
        //第一个参数Context,第二个参数资源Id,第三个参数优先级
        soundPool.load(context, rawId, 1);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                soundPool.play(1, 1, 1, 0, 0, 1);
            }
        });
        //第一个参数id,即传入池中的顺序,第二个和第三个参数为左右声道,第四个参数为优先级,第五个是否循环播放,0不循环,-1循环
        //最后一个参数播放比率,范围0.5到2,通常为1表示正常播放
//        soundPool.play(1, 1, 1, 0, 0, 1);
        //回收Pool中的资源
        //soundPool.release();
    }
}

最后通过极光平台发送消息测试发现同时弹出了极光和安卓的推送,随后就去掉了安卓本身通知channel 本身设置的声音无效,就自定义播放了一个声音。

有两个问题:
1.低于8.0以下版本应该怎么覆盖极光的声音
2.能不能在flutter禁用极光默认声音

有知道的小伙伴可以留言一起讨论下~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可鸭~

想吃糖~我会甜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值