天天看点

极光推送自定义通知栏提示音

话不多说直接上代码

/**
     * 实现自定义推送声音
     * @param context
     * @param bundle
     */

    private void processCustomMessage(Context context, Bundle bundle) {

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

        NotificationCompat.Builder notification = new NotificationCompat.Builder(context);

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

        notification.setAutoCancel(true)
                .setContentText(extras)
                .setContentTitle(context.getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher);
        if (!TextUtils.isEmpty(extras)) {
            try {
                JSONObject extraJson = new JSONObject(extras);
                if (null != extraJson && extraJson.length() > 0) {

                    String sound = extraJson.getString("order_type");
                    String id = extraJson.getString("id");

//                    order_type  这个1是新订单 2是取消订单
                    Log.e(TAG,sound+"---------------");
                    //根据数据选择音频
                    if("1".equals(sound)){
                        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.neworder));
                    }else if (sound.equals("2")){
                        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.cancle));
                    }

        //此处处理点击跳转效果
        Intent mIntent = new Intent(context,DetailsAty.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mIntent.putExtra(ExtraKey.ORDER_ID,id);
        mIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);

        notification.setContentIntent(pendingIntent);
        //覆盖掉原有通知
        notificationManager.notify(bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), notification.build());
            }
        } catch (JSONException e) {

        }

        }
    }
           

2、在极光推送接收通知的位置下处理

else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            //自定义消息声音
            processCustomMessage(context,bundle);

        } 
           

3、需要注意的事项:

3.1自定义通知栏时

notification.setAutoCancel(true)
                .setContentText(extras)
                .setContentTitle(context.getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher);
           

这几个属性必须设置否则就炸了

3.2 自定义通知栏之后需要覆盖掉原有通知否则会出现一条推送显示两条信息的情况

方法如下:

//覆盖掉原有通知
        notificationManager.notify(bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), notification.build());
           

该方法第一个参数为原推送通知id

注:1、如果走的是极光通知消息的话则需要极光推送内容都为空,通知栏内容都在自定义notification中展示,否则可能会在锁屏状态下收到两个推送(即锁屏下响系统默认提示音,解锁后播报自定义声音)

2、使用setSound方法设置音频可能会出现“See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case”问题,无法播报

我的解决方法

if("1".equals(sound)){
                        if (mediaPlayer1==null)
                            mediaPlayer1 = MediaPlayer.create(context, R.raw.neworder);
                        mediaPlayer1.start();
                    }else if (sound.equals("2")){
                        if (mediaPlayer2==null)
                            mediaPlayer2 = MediaPlayer.create(context, R.raw.cancle);
                        mediaPlayer2.start();
                    }
           

继续阅读