구현할 내용
- 앱 내 버튼 클릭 시 알림 전송
- 알림에서 action 수행 시 특정 화면 띄우기
NotificationManager manager;
NotificationCompat.Builder builder;
private static String CHANNEL_ID = "channel1";
private static String CHANEL_NAME = "Channel1";
public void showNoti() {
builder = null;
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 연결시키고 싶은 Activity를 넣으면 된다 ▼
Intent notificatonIntent = new Intent(this, RecordListActivity.class);
notificatonIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificatonIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
//버전 오레오 이상일 경우
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(
new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
);
builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentTitle("알림 제목");
builder.setContentText("알림 내용");
builder.setSmallIcon(R.drawable.vk_color);
builder.setContentIntent(pendingIntent);
builder.addAction(R.drawable.vk_black, "확인", pendingIntent);
builder.addAction(R.drawable.vk_black, "삭제", pendingIntent);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
Notification notification = builder.build();
manager.notify(1, notification);
}
}
원하는 위치에서 showNoti()로 불러오면 OK