본문 바로가기
IT Trend/Android

Notification. API Lv10 에 대한 고찰

by 개Foot/Dog발?! 2015. 6. 13.

안드로이드 웨어는 단독 앱 형태로 구성을 할 수 있지만, 컨셉이나 태생자체가 스마트폰의 Notification을 알려주기 위해서이다.

이 Notification을 나 처럼 다른것은 해봤어도 Notification을 직접 해본 적이 없는 경우에 도움이 되고

나를 위해 남기려고 시작해 본다.(기록은 기억보다 위대하다)


이 Notification은 알다시피 보통 상단에 알림을 사용하기 위해 사용한다

특히 API Lv 10에 부터 고찰하고자 하는 것은 Notification이 JB(Lv10)까지, ICS(Lv19) 그리고 최근 LP(Lv21)에서 각각 변화를 보이기 때문이다. 물론 Notification class의 attributes나 methods들은 지속적으로 변한다.


아주 단순했던 안드로이드JB 이전에 사용하던 단순한 버전의 Notification을 고찰해 보자.


우선 Android Developer 공식사이트에서 JB인 API Lv10에서 어떤 것을 사용할 수 있는지 보자.


Constants

 

 

 

int

DEFAULT_ALL

Use all default values (where applicable).

int

DEFAULT_LIGHTS

Use the default notification lights.

int

DEFAULT_SOUND

Use the default notification sound.

int

DEFAULT_VIBRATE

Use the default notification vibrate.

 

 

 

int

FLAG_AUTO_CANCEL

Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.

int

FLAG_FOREGROUND_SERVICE

Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.

 

 

 

int

FLAG_INSISTENT

Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.

 

 

 

int

FLAG_NO_CLEAR

Bit to be bitwise-ored into the flags field that should be set if the notification should not be canceled when the user clicks the Clear all button.

int

FLAG_ONGOING_EVENT

Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.

int

FLAG_ONLY_ALERT_ONCE

Bit to be bitwise-ored into the flags field that should be set if you would only like the sound, vibrate and ticker to be played if the notification was not already showing.

int

FLAG_SHOW_LIGHTS

Bit to be bitwise-ored into the flags field that should be set if you want the LED on for this notification.

 

 

 

int

STREAM_DEFAULT

This constant was deprecated in API level 21. Use audioAttributes instead.


Fields

 

 

 

public static final Creator<Notification>

CREATOR

Parcelable.Creator that instantiates Notification objects

 

 

 

public int

audioStreamType

This field was deprecated in API level 21. Use audioAttributes instead.

 

 

 

public PendingIntent

contentIntent

The intent to execute when the expanded status entry is clicked.

public RemoteViews

contentView

The view that will represent this notification in the expanded status bar.

public int

defaults

Specifies which values should be taken from the defaults.

public PendingIntent

deleteIntent

The intent to execute when the notification is explicitly dismissed by the user, either with the "Clear All" button or by swiping it away individually.

 

 

 

public int

flags


public PendingIntent

fullScreenIntent

An intent to launch instead of posting the notification to the status bar.

 

 

 

public int

icon

The resource id of a drawable to use as the icon in the status bar.

public int

iconLevel

If the icon in the status bar is to have more than one level, you can set this.

 

 

 

public int

ledARGB

The color of the led.

public int

ledOffMS

The number of milliseconds for the LED to be off while it's flashing.

public int

ledOnMS

The number of milliseconds for the LED to be on while it's flashing.

public int

number

The number of events that this notification represents.

 

 

 

public Uri

sound

The sound to play.

public CharSequence

tickerText

Text that summarizes this notification for accessibility services.

 

 

 

public long[]

vibrate

The pattern with which to vibrate.

 

 

 

public long

when

A timestamp related to this notification, in milliseconds since the epoch.


Public Constructors


Notification()

Constructs a Notification object with default values.


Notification(int icon, CharSequence tickerText, long when)

This constructor was deprecated in API level 11. Use Notification.Builder instead.


Notification(Parcel parcel)

Unflatten the notification from a parcel.


Public Methods

Notification

clone()

Creates and returns a copy of this Object.

int

describeContents()

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

 

 

void

setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

This method was deprecated in API level 11. Use Notification.Builder instead.

String

toString()

Returns a string containing a concise, human-readable description of this object.

void

writeToParcel(Parcel parcel, int flags)

Flatten this notification from a parcel.


보다시피 아주 제한적이고 간단하다. Lv9에서는 Notification Builder등 Inner Class들의 지원이 없다.


contentIntent: Lv11에서 deprecated된 method인 setLatestEventInfo()에서 설정되는 PendingIntent나, 해당 attributes를 직접 설정하나 같다.

contentView: Notification에 보여줄 View를 지정하는 것으로 RemoteView의 인스턴스를 넘겨주면 된다.

deleteIntent: 해당 Notification이 사라질 때, 작동시키기 위한 PendingIntent의 인스턴스를 지정한다

fullScreenIntent: 풀스크린일 때, Status Bar에 표시되는 것 대신에 하는 것으로 보이나, 테스트가 필요해 보인다.
ledOffMS: LED를 꺼질 시간(ms후)

ledOnMS: LED를 켜질 시간(ms후)

ledARGB:  LED 색상 지정

sound: 소리를 내기 위한 리소스 Uri를 지정

tickerText: Status Bar에 Noti가 발생하여 보여줄 당시 보여줄 Text를 지정

vibrate: 진동주기(ms)를 설정

when: Notification이 보여질 시간 System.currentTimeMills()를 입력하면 현재 보여진다


Notification(int icon, CharSequence tickerText, long when)

    : Lv11에서 deprecated가 되었다. Attribute에서 설정가능한 icon, ticketText, when을 argument로 받

setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

    : 이 역시 Lv11에서 deprecated가 되었다. Notification Bar에서 보여질 text를 지정하는 contentTitle, 아래 내용을 보여줄 contentText, Attribute에서 contentIntent와 동일하게 기능을 하는 contentIntent를 받아 설정하는 methos이다.


위의 것들을 활용하여 NotificationManager를 통해 notification을 발생시킬 수 있다.

위에서 Lv11에서 deprecated되는 methods는 Notification.Builder를 통해 쓸 것을 권고하고 있다.



위 코드들을 이용하여 테스트 코드를 만들어 보자.