o android notification sound is not playing

General Tech Bugs & Fixes 3 years ago

5.33K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

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
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago

 

Try this it will work for you.

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
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum