-1

I'm developing my first android app, but a problem is coming. I have setted a notification sound with Firebase. My app currently works, it play the sound only when app is opened on the screen... If the screen is locked, I get the notification on my screen, but without playing the sound... how can I resolve it?

Here's a part of the code: https://imghost.io/images/2017/09/19/Cattura.png

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
loreeemartiii
  • 355
  • 5
  • 14

2 Answers2

0

Try this.

try {
    // Get default notification uri object.
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
} catch (Exception e) {
    e.printStackTrace();
}

Then set this uri as your sound in the notification builder object like

notificationBuilder.setSound(notification)

View this original answer

shaheer_
  • 406
  • 1
  • 4
  • 12
0
private void showNotification(String msg) 
{
        private Context mContext = getApplicationContext();
        private NotificationManager  mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);



        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> services = activityManager
                .getRunningTasks(Integer.MAX_VALUE);
        boolean isActivityFound = false;

        if (services.get(0).topActivity.getPackageName().toString()
                .equalsIgnoreCase(getPackageName().toString())) {
            isActivityFound = true;
        }
        Intent openIntent = null;
        if (isActivityFound) {
            openIntent = new Intent();
        } else {
            openIntent = new Intent(this, MainActivity.class);
            openIntent.putExtra("message", msg);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                openIntent, PendingIntent.FLAG_ONE_SHOT);


        if (msg != null && (!msg.isEmpty())) {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setVibrate(new long[]{100, 250, 100, 250, 100, 250})
                            .setAutoCancel(true)
                            .setColor(getResources().getColor(R.color.activity_toolbar_color))
                            .setContentTitle(theTitle)
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(Html.fromHtml(msg)))
                            .setPriority(Notification.PRIORITY_MAX)
                            .setContentText(Html.fromHtml(msg));

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mBuilder.setSmallIcon(R.drawable.notification_icon1);
            } else {
                mBuilder.setSmallIcon(R.drawable.notification_icon);
            }

            mBuilder.setContentIntent(contentIntent);


            int notificationNumber = 0;

            mNotificationManager.notify(notificationNumber, mBuilder.build());

        }
    }
jessica
  • 1,700
  • 1
  • 11
  • 17