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
'Computer Science > Android Application' 카테고리의 다른 글
Android wear app (0) | 2015.08.25 |
---|---|
Android Studio 자동 import 기능 (0) | 2015.07.15 |
Android Wear 개발환경 구축 (0) | 2015.07.15 |
Android Studio 특징 및 단축키 (0) | 2015.05.24 |
다중 스크린 사이즈를 지원하는 방법 (0) | 2014.10.27 |