• 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 版本为例。
    
    • 1
    • 2

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

            jcenter()
    
    • 1

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

        <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" />
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    配置原生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());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

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

    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());
            }
        }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

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

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

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

  • 相关阅读:
    Nacos集群搭建
    Win11提示Windows无法访问指定设备路径或文件的三种解决方法
    前端实现分页效果
    Flink入门系列01-概述
    第七届信息类研究生学术论坛参赛有感
    TMS Sphinx Alexandria Full Source
    信号分解 | SSA(奇异谱分析)-Matlab
    【数据结构与算法】时间复杂度和空间复杂度
    使用 HTML、CSS 和 JavaScript 的实时计算器
    Java多线程篇(8)——AQS之条件等待队列(CyclicBarrier)
  • 原文地址:https://blog.csdn.net/qq_43547255/article/details/126430288