import
static
android.content.Context.NOTIFICATION_SERVICE;
import
android.app.Notification;
import
android.app.NotificationChannel;
import
android.app.NotificationManager;
import
android.app.PendingIntent;
import
android.content.Context;
import
android.media.RingtoneManager;
import
android.net.Uri;
import
android.os.Build;
import
androidx.core.app.NotificationCompat;
public
class
NotificationBuilder {
private
static
final
String CHANNEL_ID =
"default"
;
private
static
final
String CHANNEL_NAME =
"기본채널"
;
private
static
final
long
[] DEFAULT_VIBRATE = {
0
,
100
,
200
,
300
};
private
static
final
int
NOTIFICATION_ID =
2
;
private
String channelId = CHANNEL_ID;
private
String channelName = CHANNEL_NAME;
private
int
notificationId = NOTIFICATION_ID;
private
Uri ringtoneUri;
private
long
[] vibrationPattern = DEFAULT_VIBRATE;
private
PendingIntent pendingIntent;
private
String title;
private
String message;
private
boolean
autoCancel =
true
;
private
final
Context context;
public
NotificationBuilder(Context context) {
this
.context = context;
this
.ringtoneUri = getDefaultRingtoneUri();
this
.pendingIntent = NoOpBroadcastReceiver.getPendingIntent(context);
this
.title = context.getString(R.string.app_name);
this
.message = context.getString(R.string.app_name);
}
public
NotificationBuilder setChannelId(String channelId) {
this
.channelId = channelId;
return
this
;
}
public
NotificationBuilder setChannelName(String channelName) {
this
.channelName = channelName;
return
this
;
}
public
NotificationBuilder setRingtoneUri(Uri ringtoneUri) {
this
.ringtoneUri = ringtoneUri;
return
this
;
}
public
NotificationBuilder setVibrationPattern(
long
[] vibrationPattern) {
this
.vibrationPattern = vibrationPattern;
return
this
;
}
public
NotificationBuilder setPendingIntent(PendingIntent pendingIntent) {
this
.pendingIntent = pendingIntent;
return
this
;
}
public
NotificationBuilder setTitle(String title) {
this
.title = title;
return
this
;
}
public
NotificationBuilder setMessage(String message) {
this
.message = message;
return
this
;
}
public
NotificationBuilder setNotificationId(
int
notificationId) {
this
.notificationId = notificationId;
return
this
;
}
public
NotificationBuilder setAutoCancel(
boolean
autoCancel) {
this
.autoCancel = autoCancel;
return
this
;
}
public
Notification build() {
final
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(context, channelId);
builder.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setSound(ringtoneUri)
.setVibrate(vibrationPattern)
.setAutoCancel(autoCancel);
NotificationManager manager = getNotificationManager();
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(
new
NotificationChannel(channelId, channelName,
NotificationManager.IMPORTANCE_DEFAULT));
}
Notification notification = builder.build();
manager.notify(notificationId, notification);
return
notification;
}
private
NotificationManager getNotificationManager() {
return
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
}
private
Uri getDefaultRingtoneUri() {
return
RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
}
}