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.
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 =newAudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);String id ="my_channel_01";CharSequence name ="oreiomilla";Stringdescription="i love me";int importance =NotificationManager.IMPORTANCE_HIGH;NotificationChannel mChannel =newNotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel .setSound(alarmSound,attributes);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(newlong[]{100,200,300,400,500,400,300,200,400});
mNotificationManager.createNotificationChannel(mChannel);int notifyID =1;String CHANNEL_ID ="my_channel_01";Intent intent =newIntent(getApplicationContext(),MainActivity.class);PendingIntent pIntent =PendingIntent.getActivity(getApplicationContext(),0, intent,0);Notification notification =newNotification.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
privatevoidBigTextNotificationForDays(Contextcontext,StringMessage,StringAuthor){Intent resultIntent =newIntent(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 notificationNotificationCompat.BigTextStyle bigText =newNotificationCompat.BigTextStyle();
bigText.bigText(Message);
bigText.setBigContentTitle(context.getString(R.string.app_name));
bigText.setSummaryText(Author);//build notificationNotificationCompat.Builder mBuilder =newNotificationCompat.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(newlong[]{1000,1000}).setSound(Settings.System.DEFAULT_NOTIFICATION_URI).setPriority(Notification.PRIORITY_HIGH).setAutoCancel(true).setContentIntent(pendingIntent);//.setOngoing(true);// Gets an instance of the LocalNotificationManager serviceNotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);//to post your notification to the notification bar
mNotificationManager.notify(3730, mBuilder
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.
manpreet
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?