diff --git a/Client/TeamTalkAndroid/src/main/java/dk/bearware/backend/TeamTalkService.java b/Client/TeamTalkAndroid/src/main/java/dk/bearware/backend/TeamTalkService.java index d29d261b11..3e03b6a526 100644 --- a/Client/TeamTalkAndroid/src/main/java/dk/bearware/backend/TeamTalkService.java +++ b/Client/TeamTalkAndroid/src/main/java/dk/bearware/backend/TeamTalkService.java @@ -51,6 +51,8 @@ import android.view.KeyEvent; import android.widget.Toast; +import androidx.core.app.NotificationCompat; + import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -144,6 +146,7 @@ public class TeamTalkService extends Service private static final int UI_WIDGET_ID = 1; private static final String UI_WIDGET_TAG = "tt5_ui_widget"; + private static final String UI_CHANNEL_ID = "TeamtalkConnection"; // Binder given to clients private final IBinder mBinder = new LocalBinder(); @@ -156,8 +159,8 @@ public class TeamTalkService extends Service private boolean voxSuspended; private boolean permanentMuteState; private boolean currentMuteState; - Notification.Builder widget = null; - NotificationManager notificationManager; + private Notification widget = null; + private NotificationManager notificationManager; private volatile boolean inPhoneCall; private MediaSessionCompat mediaSession; Handler reconnectHandler = new Handler(); @@ -378,12 +381,11 @@ private String getNotificationText() { private void displayNotification(boolean enabled) { if (enabled) { if (widget == null) { + notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent ui = new Intent(this, MainActivity.class); ui.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - widget = new Notification.Builder(this); - notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel mChannel = new NotificationChannel("TeamtalkConnection", "Teamtalk connection", NotificationManager.IMPORTANCE_DEFAULT); + NotificationChannel mChannel = new NotificationChannel(UI_CHANNEL_ID, "Teamtalk connection", NotificationManager.IMPORTANCE_DEFAULT); mChannel.enableVibration(false); mChannel.setVibrationPattern(null); mChannel.enableLights(false); @@ -391,20 +393,21 @@ private void displayNotification(boolean enabled) { notificationManager.createNotificationChannel(mChannel); } int intentFlags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE; - widget.setSmallIcon(R.drawable.teamtalk_green) + widget = new NotificationCompat.Builder(this, UI_CHANNEL_ID) + .setSmallIcon(R.drawable.teamtalk_green) .setContentTitle(getString(R.string.app_name)) .setContentIntent(PendingIntent.getActivity(this, 0, ui, intentFlags)) .setOngoing(true) .setAutoCancel(false) - .setContentText(getNotificationText()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - widget.setChannelId("TeamtalkConnection"); - } - widget.setShowWhen(false); + .setContentText(getNotificationText()) + .setShowWhen(false) + .build(); } else { - widget.setContentText(getNotificationText()); + widget = new NotificationCompat.Builder(this, widget) + .setContentText(getNotificationText()) + .build(); } - notificationManager.notify(UI_WIDGET_TAG, UI_WIDGET_ID, widget.build()); + notificationManager.notify(UI_WIDGET_TAG, UI_WIDGET_ID, widget); } else if (widget != null) { notificationManager.cancel(UI_WIDGET_TAG, UI_WIDGET_ID); widget = null; diff --git a/Client/TeamTalkAndroid/src/main/java/dk/bearware/gui/MainActivity.java b/Client/TeamTalkAndroid/src/main/java/dk/bearware/gui/MainActivity.java index dbe332d1b2..191bf066d6 100644 --- a/Client/TeamTalkAndroid/src/main/java/dk/bearware/gui/MainActivity.java +++ b/Client/TeamTalkAndroid/src/main/java/dk/bearware/gui/MainActivity.java @@ -85,6 +85,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.NotificationCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; @@ -168,6 +169,8 @@ public class MainActivity public static final String TAG = "bearware"; + private static final String MSG_NOTIFICATION_CHANNEL_ID = "TT_PM"; + public final int REQUEST_EDITCHANNEL = 1, REQUEST_NEWCHANNEL = 2, REQUEST_EDITUSER = 3, @@ -2166,24 +2169,22 @@ else if (textmessage.nFromUserID == ttservice.getTTInstance().getMyUserID()) { if (ttsWrapper != null && prefs.getBoolean("private_message_checkbox", false)) ttsWrapper.speak(getString(R.string.text_tts_private_message, senderName, textmessage.szMessage)); Intent action = new Intent(this, TextMessageActivity.class); - Notification.Builder notification = new Notification.Builder(this); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel mChannel = new NotificationChannel("TT_PM", "Teamtalk incoming message", NotificationManager.IMPORTANCE_HIGH); + NotificationChannel mChannel = new NotificationChannel(MSG_NOTIFICATION_CHANNEL_ID, "Teamtalk incoming message", NotificationManager.IMPORTANCE_HIGH); mChannel.enableVibration(false); mChannel.setVibrationPattern(null); mChannel.enableLights(false); mChannel.setSound(null, null); notificationManager.createNotificationChannel(mChannel); } - notification.setSmallIcon(R.drawable.message) + Notification notification = new NotificationCompat.Builder(this, MSG_NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.message) .setContentTitle(getString(R.string.private_message_notification, senderName)) .setContentText(getString(R.string.private_message_notification_hint)) .setContentIntent(PendingIntent.getActivity(this, textmessage.nFromUserID, action.putExtra(TextMessageActivity.EXTRA_USERID, textmessage.nFromUserID), PendingIntent.FLAG_IMMUTABLE)) - .setAutoCancel(true); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notification.setChannelId("TT_PM"); - } - notificationManager.notify(MESSAGE_NOTIFICATION_TAG, textmessage.nFromUserID, notification.build()); + .setAutoCancel(true) + .build(); + notificationManager.notify(MESSAGE_NOTIFICATION_TAG, textmessage.nFromUserID, notification); break; case TextMsgType.MSGTYPE_CUSTOM: default: