Notification sound does not work for api 10 android

I use this function to display status notification. Everything is correct, but the sound notificationdoes not play.

public void notifiction_main(String ticker,String title,String text,int _icon){

        String ns = mContext.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(ns);
        int icon = R.id.icon;
        CharSequence tickerText = ticker; // ticker-text
        long when = System.currentTimeMillis();
        CharSequence contentTitle = title;
        CharSequence contentText = text;
        Intent notificationIntent = new Intent(mContext, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.setLatestEventInfo(mContext, contentTitle, contentText,contentIntent);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify(1, notification);


    }

I set the vibration after that, but the vibration didn't work either :(

+4
source share
2 answers

you can use NotificationCompat.Builder

NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("your_message")
    .setContentIntent(pendingIntent)
    .setAutoCancel(true)
    .setPriority(NotificationCompat.PRIORITY_HIGH);

notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
long[] pattern = {500,500,500,500};
notification.setVibrate(pattern);
+3
source

You should use NotificationCompat.Builder as the Notification class is stripped of API level 20 and above

and use this piece of code, I hope it will work for u

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.drawable.notificationg);
        Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
                R.drawable.ic_launcher);
        builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(icon));
        Intent i = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i, 0);

        builder.addAction(R.drawable.ic_launcher,"OK",pIntent);
        // This intent is fired when notification is clicked
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle(notificationTitle);

        // Content text, which appears in smaller text below the title
        builder.setContentText(notificationMessage);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);
        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to go to link in notifications.");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIFICATION_COUNT, builder.build());

The sound signal will work normally Uri alarmSound = RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION); builder.setSound (alarmSound);

+2

Source: https://habr.com/ru/post/1619171/


All Articles