Skip to content

Commit

Permalink
Merge pull request #74 from brarcher/notification-fix
Browse files Browse the repository at this point in the history
Set a notification channel for FFmpeg service
  • Loading branch information
brarcher authored May 30, 2018
2 parents afd427a + c20e562 commit 9a8933e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 36 additions & 1 deletion app/src/main/java/protect/babysleepsounds/AudioService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package protect.babysleepsounds;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

Expand All @@ -15,6 +21,7 @@ public class AudioService extends Service
private final static String TAG = "BabySleepSounds";

private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = TAG;

public static final String AUDIO_FILENAME_ARG = "AUDIO_FILENAME_ARG";

Expand Down Expand Up @@ -64,8 +71,14 @@ public int onStartCommand(Intent intent, int flags, int startId)

private void setNotification()
{
String channelId = "";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
channelId = createNotificationChannel();
}

NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
new NotificationCompat.Builder(this, channelId)
.setOngoing(true)
.setSmallIcon(R.drawable.playing_notification)
.setContentTitle(getString(R.string.app_name))
Expand All @@ -81,6 +94,28 @@ private void setNotification()
startForeground(NOTIFICATION_ID, builder.build());
}

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel()
{
String channelId = NOTIFICATION_CHANNEL_ID;
String channelName = getString(R.string.notificationChannelName);
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(service != null)
{
service.createNotificationChannel(chan);
}
else
{
Log.w(TAG, "Could not get NotificationManager");
}

return channelId;
}


@Override
public void onDestroy()
{
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

<string name="notificationPlaying">Playing&#8230;</string>

<string name="notificationChannelName">Baby Sleep Sounds Audio Service</string>

<string name="about">About</string>
<string name="app_copyright_fmt">Copyright 2016-<xliff:g>%d</xliff:g> Branden Archer</string>
<string name="app_license">Licensed under the GPLv3.</string>
Expand Down

0 comments on commit 9a8933e

Please sign in to comment.