public
class
NotificationService
extends
Service {
@Nullable
@Override
public
IBinder onBind(Intent intent) {
return
null
;
}
@Override
public
int
onStartCommand(Intent intent,
int
flags,
int
startId) {
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = createNotificationChannel();
Intent testIntent =
new
Intent(getApplicationContext(), MainActivity.
class
);
PendingIntent pendingIntent
= PendingIntent.getActivity(
this
,
0
, testIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(
this
, channelId);
Notification notification = builder.setOngoing(
true
)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(
"일정"
)
.setContentText(
"일정 내용"
)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
.setAutoCancel(
true
)
.addAction(R.drawable.ic_button_add,
"확인"
, pendingIntent)
.build();
startForeground(
1
, notification);
}
return
START_STICKY;
}
@RequiresApi
(Build.VERSION_CODES.O)
private
String createNotificationChannel() {
String channelId =
"Alarm"
;
String channelName = getString(R.string.app_name);
NotificationChannel channel =
new
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
channel.setSound(
null
,
null
);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
return
channelId;
}
}