첫번째 문제는 해결했는데 혹시 필요하신 분 있으실까봐 답변남깁니다.
일단 setAutoCancel이 작동하지 않았던 것은 알림을 띄운 것이 foreground로 계속 작동해서 그랬던 것 같습니다.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Foreground 에서 실행되면 Notification 을 보여줘야 됨
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Oreo(26) 버전 이후 버전부터는 channel 이 필요함
String channelId = createNotificationChannel();
Intent testIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent
= PendingIntent.getActivity(this, 0, testIntent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent2
= PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
Notification notification = builder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("일정")
.setContentText("일정 내용")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
.addAction(R.drawable.ic_button_add, "확인", pendingIntent)
.addAction(R.drawable.ic_button_add, "취소", pendingIntent2);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(0, builder.build());
startForeground(1, notification);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
stopSelf();
return START_STICKY;
}
위처럼 notification이랑 띄울 알림을 따로 구분해두고 startForeground에는 notification을 넣은 후 바로 stopForeground를 사용해서 없애고
띄울 알림은 NotificationManager.notify로 띄워줬더니 해결됐습니다.
확인버튼은 메인액티비티에
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
위 코드를 추가하고
CancelNotification(this, 0);
onCreate에 위 코드를 입력했습니다.
알림 등록할 때 등록한 notifyId를 입력해서 메인액티비티가 등장하면 해당 알림을 종료시켰습니다.
취소버튼도 위 방법대로 하면 될 것 같은데 아직 방법을 찾고있습니다.
이것보다 좋은 방법이나 취소버튼 구현방법을 알고계시면 답변 부탁드립니다.