Home 상단 알림으로 알려주기
Post
Cancel

상단 알림으로 알려주기

상단 알림으로 알려주기

알림은 화면 상단에 정보를 표시하여 사용자가 알 수 있도록 한다.

이 알림 기능은 주로 다른 사람에게서 메시지를 받았을 때나 단말의 상태를 표시할 때 사용한다.

예를 들어 카카오톡 앱을 직접 만든다면 앱이 실행되지 않은 상태에서도 사용자에게 메시지가 왔다는 것을 알려줄 필요가 있다.

이때 백그라운드에서 동작하는 서비스에서 알림을 표시하면 사용자에게 알려줄 수 있다.

알림은 NotificationManager 시스템 서비스를 이용해 화면 상단에 띄울 수 있다.

알림을 띄우려면 Notification 객체를 만들어야 하는데 이 객체는 NotificationCompat.Builder 객체를 이용해서 만들 수 있다.


예제

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.techtown.noti;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    NotificationManager manager;

    private static String CHANNEL_ID = "channel1";
    private static String CHANNEL_NAME = "Channel1";

    private static String CHANNEL_ID2 = "channel2";
    private static String CHANNEL_NAME2 = "Channel2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNoti1();
            }
        });

        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNoti2();
            }
        });
    }

    public void showNoti1() {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.createNotificationChannel(new NotificationChannel(
                    CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
            );

            builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(this);
        }

        builder.setContentTitle("간단 알림");
        builder.setContentText("알림 메시지입니다.");
        builder.setSmallIcon(android.R.drawable.ic_menu_view);
        Notification noti = builder.build();

        manager.notify(1, noti);
    }

    public void showNoti2() {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.createNotificationChannel(new NotificationChannel(
                    CHANNEL_ID2, CHANNEL_NAME2, NotificationManager.IMPORTANCE_DEFAULT
            ));

            builder = new NotificationCompat.Builder(this, CHANNEL_ID2);
        } else {
            builder = new NotificationCompat.Builder(this);
        }

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 101, intent,
                                                                PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentTitle("간단 알림");
        builder.setContentText("알림 메세지 입니다.");
        builder.setSmallIcon(android.R.drawable.ic_menu_view);
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);

        Notification noti = builder.build();

        manager.notify(2, noti);
    }
}

showNoti1 메서드 안에 NotificationManager 객체를 참조한 후 NotificationCompat.Builder 객체를 생성한다.

안드로이드 오레오 버전 이전과 이후에 Builder 객체를 만드는 방법이 다르기 때문에 Build.VERSION.SDK_INT 상수의 값을 비교하여 단말의 OS 버전에 따라 다른 코드가 실행되도록 한다.

오레오 버전 이후 버전에서는 알림 채널이 지정되어야 하며, 채널은 createNotificationChannel메서드를 이용해 생성할 수 있다.

Builder 객체가 만들어지면 알림 정보를 설정할 수 있고 Builder 객체의 build 메서드를 호출하면 Notification 객체가 생성된다.

NotificationManager의 notify 메서드를 호출하면서 이 Notification 객체를 매개변수로 전달하면 알림을 띄우게 된다.

또한 showNoti2 메서드는 버튼을 눌렀을 때PendingIntent를 만들어 Notification을 만들 때 설정한다. PedingIntentIntent와 비슷하지만 시스템에서 대기하는 역할을한다.

예를 들어 액티비티를 띄우는 역할을 하는 메서드가 startActivity 또는 startActivityForResult인데 이 메서드를 호출하면 시스템에서는 즉시 해석하고 처리한다.

하지만 PendingIntent는 지정된 상황이 될 때까지 보관하고 있게 된다.

setAutoCancel 메서드는 알림을 클릭했을 때 자동으로 알림 표시를 삭제하라는 설정이다.

setContentIntent 메서드에는 PendingIntent 객체가 매개 변수로 전달된다. 그리고 PendingIntent 객체에는 Intent 객체가 매개 변수로 전달되는데 이렇게 되면 알림을 클릭했을 때 이 Intent 객체를 이용해 액티비티를 띄워준다. 액티비티는 MainActivity가 뜨도록 MainActivity.class로 지정했다.


결과

첫 번째 버튼을 클릭하면 상단 바에 알람이 띄워지게 되고 두 번째 버튼을 클릭하면 상단 바에 알람이 띄워지고 알림을 클릭하면 MainActivity 화면으로 돌아온다.

ezgif com-gif-maker


알름을 표시하는 방법은 이것 외에도 글자를 많이 표시하거나 이미지를 표시하거나 또는 목록을 표시하는 방법 등이 있다.

이것을 스타일 알림 (Styled Notification) 이라고 부른다.

This post is licensed under CC BY 4.0 by the author.