푸시알림이 아닌 앱 자체에서 알림을 발생


시키고 싶을 땐 Notification을 사용하면 


됩니다.


다음 코드를 통해 간단하게 작성가능합니다.




NotificationManager notificationManager;


PendingIntent intent;



먼저 NotificationMangerPendingIntent 객체를 하나씩 선언해줍니다.


NotificationManger는 notification(상태알림창의 알림)을 사용하기 위한 객체이며,


PendingIntent는 notification을 상태알림창에 띄울 intent를 뜻합니다.



가장 간단한 테스트로 onCreate 함수 내에 코드를 작성했습니다.


PendingIntent 객체인 intent는 다음과 같이 정의합니다.



intent = PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), MainActivity.class),

PendingIntent.FLAG_UPDATE_CURRENT);



Notification 내용을 설정하기 위해 Notifiaction.Builder를 이용하여 빌더를 만들어줍니다.



Notification.Builder builder = new Notification.Builder(this)

                .setSmallIcon(R.drawable.ic_launcher_background) // 아이콘 설정하지 않으면 오류남

                .setDefaults(Notification.DEFAULT_ALL)

                .setContentTitle("알림 제목") // 제목 설정

                .setContentText("알림 내용") // 내용 설정

                .setTicker("한줄 출력") // 상태바에 표시될 한줄 출력

                .setAutoCancel(true)

                .setContentIntent(intent);



제목, 내용 등을 다양하게 설정할 수 있습니다. 


이상하게도 아이콘을 설정해 주지않으면 오류가 나오는것을 확인할 수 있었습니다.


그래서 기본 아이콘 아무거나 가져와서 사용했습니다....



다음은 notificationManger 변수를 다음과 같이 정의해줍니다.



notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, builder.build());



자 이것으로 알림(Notification) 기능 설정이 모두 끝났습니다.


테스트 코드는 onCreate함수에 모두 집어넣었기 때문에 프로그램 실행 시 바로 알림이 


뜨는것을 확인할 수 있었습니다.





Notification 예제 - 전체 코드입니다.


import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Build;

import android.support.annotation.RequiresApi;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    NotificationManager notificationManager;

    PendingIntent intent;


    @RequiresApi(api = Build.VERSION_CODES.M)

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        intent = PendingIntent.getActivity(this, 0,

                new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);


        Notification.Builder builder = new Notification.Builder(this)

                .setSmallIcon(R.drawable.ic_launcher_background) // 아이콘 설정하지 않으면 오류남

                .setDefaults(Notification.DEFAULT_ALL)

                .setContentTitle("알림 제목") // 제목 설정

                .setContentText("알림 내용") // 내용 설정

                .setTicker("한줄 출력") // 상태바에 표시될 한줄 출력

                .setAutoCancel(true)

                .setContentIntent(intent);


        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, builder.build());

    }

}





이상 '안드로이드 notification 사용법'에 대해 알아보았습니다.


질문 또는 오타나 잘못된 정보가 있는 경우 댓글로 달아주세요!


공감♡ 버튼을 눌러주시면 더욱 유용하고 좋은 포스팅으로 찾아 뵙겠습니다.






to Top