Support Library version 4를 이용해서 호환을 함.


NotificationCompat.Builder



5.0 material design의 영향으로 해당 버전에서는 약간 다르다.



Creating a Notification


NotificationCompat.Builder

NotificiationCOmpat.Builder.build() => notification object


NotificationManager.notify() // notification object will be passed to the system by calling that function.



Required notification contents


setSmallIcon() // A small icon 

setContentTitle() // a title

setContentText() // Detail text


그밖에도 많은 options들이 API를 보면 나와 있다.


Notification actions


하나 이상의 action은 반드시 정의 되어함.

특정 activity로 이동하는 action을 의미한다.


두개 이상의 action을 수행하기 위해서는 4.1 이상이 되어야함.

snoozing alarm 또는 send a text 등과 같은 것들이다.


PendingIntent

setContentIntent()


Notification Priority


priority를 통해서 notification이 언제 어떻게 보일지를 설정 할 수 있다.


NotificationCompat.Builder.setPriority()

PRIORITY_MIN (-2)

PRIORITY_MAX (2)

PRIORITY_DEFAULT (0)



Creating a simple notification


눌렀을 때 어떤 activity가 실행 되는 간단한 코드는 아래와 같다.

TaskStackBuilder는 간단히 PendingIntent를 생성 하는 것이다.


NotificationCompat.Builder mBuilder =
       
new NotificationCompat.Builder(this)
       
.setSmallIcon(R.drawable.notification_icon)
       
.setContentTitle("My notification")
       
.setContentText("Hello World!");

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder
.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder
.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder
.getPendingIntent(
           
0,
           
PendingIntent.FLAG_UPDATE_CURRENT
       
);
mBuilder
.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
   
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());


1) 간단히 object를 생성

2) 눌렀을 때 시작되는 intent를 생성

3) Back forward 했을때 문제를 막기 위해서 home screen을 보장하는 intent를 생성

4) 시스템에 등록

5) notify를 통해서 최종적으로 시스템에 등록



Applying an expanded layout to a notification

4.1 이상에서만 지원



Handling Compatibility

파편화로 인해서 당연히 이러한 부분도 고려를 해야함.



Managing Notifications 

When you need to issue a notification multiple times for the same type of event, you should avoid making a completely new notification.
Instead, you should consider updating a previous notification, either by changing some of its values or by adding to it, or both.

-> updating notifications

-> removing notifications



Preserving Navigation when Starting an Activity







Notification priority



MAX 긴급하고 타임 크리티컬 하다. 지속적으로 계속 해결해야하는 테스크일때 사용 
HIGH 중요한 커뮤니케이셔이다. 메시지나 채팅 메시지 같은 것이다. 중요한 것들이라면 heads-up display를 만들어 내게 된다.
Default: 어디에도 속하지 않을 때 기본적으로 사용함.
LOW 그냥 보기는 원하지는 급하진 않은것들.
거의 bottom list에만 출력하게 된다.
MIN 날씨 정보나 상황 정보 같은 것드이다. status bar에 보이지 않는다. 
status bar를 펼쳐야 보이게 된다.

Notification  Manager에서 각각에 따라서 행동을 정의해서 동작해 주는것 같다.
이미 어느정도 고려하는 부분이 있는 것이다.





+ Recent posts