[안드로이드/Android] AlarmManager로 알림 기능 구현하기

SooYeon Yeon·2022년 5월 6일
0

안드로이드/Android

목록 보기
25/25

https://aries574.tistory.com/137

블로그 참조

NotificationHelper.java


public class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelNm = "chanelNm";

    private NotificationManager notificationManager;

    public NotificationHelper(Context base) {
        super(base);
        // 안드로이드 버전 오레오 or 이상이면 채널생성
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannels();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createChannels() {
        NotificationChannel channel1 = new NotificationChannel(channelID,channelNm,NotificationManager.IMPORTANCE_DEFAULT);
        channel1.enableLights(true);
        channel1.enableVibration(true);
        channel1.setLightColor(R.color.design_default_color_on_primary);
        channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(channel1);
    }

    public NotificationManager getManager() {
        if (notificationManager == null) {
            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notificationManager;
    }

    public NotificationCompat.Builder getChannelNotification() {
        return new NotificationCompat.Builder(getApplicationContext(),channelID)
                .setContentTitle("알람")
                .setContentText("알람매니저 실행중")
                .setSmallIcon(R.drawable.ic_launcher_background);
    }
}

AlertReceiver.java

public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1,nb.build());
    }
}

알림 기능을 구현할 준비는 끝났다.

MainActivity.java

private void updateTimeText() {
        String timeText = "60초 뒤 알람이 울려요 :>";
        Toast.makeText(ContextStorage.getCtx(), timeText, Toast.LENGTH_SHORT).show();
    }

    private void startAlarm(String st) {
        AlarmManager alarmManager = (AlarmManager) ContextStorage.getCtx().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ContextStorage.getCtx(), AlertReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(ContextStorage.getCtx(),1,intent,0);
				//60초 뒤 실행
        alarmManager.set(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime()+60*1000,pendingIntent);

    }

    private void cancelAlarm() {
        AlarmManager alarmManager = (AlarmManager) ContextStorage.getCtx().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ContextStorage.getCtx(), AlertReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(ContextStorage.getCtx(),1,intent,0);
        alarmManager.cancel(pendingIntent);
        Toast.makeText(ContextStorage.getCtx(), "알람 취소 !", Toast.LENGTH_SHORT).show();
    }

0개의 댓글