o android 通知声音没有播放 [英] o android notification sound is not playing

查看:29
本文介绍了o android 通知声音没有播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我发出通知的代码,通知显示但不播放声音请帮助我识别代码中的错误,我们是否需要任何权限才能播放声音、振动通知?

This is my code for making notification and notification is showing but not playing sound please help me to identify mistakes in my code and do we need any permission to play sound,vibrate for notification?

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String id = "my_channel_01";
            CharSequence name = "oreiomilla";
            String description ="i love me";
            int importance = NotificationManager.IMPORTANCE_HIGH; 
            NotificationChannel mChannel = new NotificationChannel(id, name, importance); 
            mChannel.setDescription(description); 
            mChannel.enableLights(true); 
            mChannel.setLightColor(Color.RED);
            mChannel .setSound(alarmSound,attributes);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
            mNotificationManager.createNotificationChannel(mChannel); 
            int notifyID = 1; 
            String CHANNEL_ID = "my_channel_01";

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
            Notification notification = new Notification.Builder(MainActivity.this)
                    .setContentTitle("New Message")
                    .setContentText("You've received new messages. "+ct)
                    .setSmallIcon(R.drawable.ic_app_icon)
                    .setChannelId(CHANNEL_ID)   .setTicker("Showing button notification") //
                    .addAction(android.R.drawable.ic_dialog_alert, "Visit", pIntent) // accept notification button
                    .addAction(android.R.drawable.ic_dialog_email, "ignore", pIntent)
                    .build();
            mNotificationManager.notify(notifyID, notification);

推荐答案

试试这个它对你有用.

private void BigTextNotificationForDays(Context context, String Message, String Author) {
            Intent resultIntent = new Intent(context, SplashActivity.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) Calendar.getInstance().getTimeInMillis(), resultIntent, PendingIntent.FLAG_ONE_SHOT);

            //To set large icon in notification
            //  Bitmap icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.big_image);

            //Assign inbox style notification
            NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
            bigText.bigText(Message);
            bigText.setBigContentTitle(context.getString(R.string.app_name));
            bigText.setSummaryText(Author);
            //build notification
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,createNotificationChannel(context))
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentText(Message)
                    .setStyle(bigText)
                    .setLargeIcon(icon)
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    .setVibrate(new long[]{1000, 1000})
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
            //.setOngoing(true);

            // Gets an instance of the LocalNotificationManager service
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            //to post your notification to the notification bar
            mNotificationManager.notify(3730, mBuilder.build()); // (int) System.currentTimeMillis() set instead of id. if at a time more notification require
        }


public static String createNotificationChannel(Context context) {

        // NotificationChannels are required for Notifications on O (API 26) and above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            // The id of the channel.
            String channelId = "Channel_id";

            // The user-visible name of the channel.
            CharSequence channelName = "Application_name";
            // The user-visible description of the channel.
            String channelDescription = "Application_name Alert";
            int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
            boolean channelEnableVibrate = true;
//            int channelLockscreenVisibility = Notification.;

            // Initializes NotificationChannel.
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
            notificationChannel.setDescription(channelDescription);
            notificationChannel.enableVibration(channelEnableVibrate);
//            notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);

            // Adds NotificationChannel to system. Attempting to create an existing notification
            // channel with its original values performs no operation, so it's safe to perform the
            // below sequence.
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);

            return channelId;
        } else {
            // Returns null for pre-O (26) devices.
            return null;
        }
    }

这篇关于o android 通知声音没有播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆