Skip to content

Commit

Permalink
NotificationCompat API utilization
Browse files Browse the repository at this point in the history
  • Loading branch information
poretsky authored and bear101 committed Mar 23, 2024
1 parent 93e5167 commit d535632
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -378,33 +381,33 @@ 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);
mChannel.setSound(null, null);
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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d535632

Please sign in to comment.