Notice
Recent Posts
Recent Comments
반응형
올해는 머신러닝이다.
[펌] Notification 알람 관련 팁 본문
반응형
출처 : http://icess.egloos.com/3341178
알림기능을 다루기 위해서는 시스템 서비스인 NotificationManager와 Notification객체를 사용한다.

알림기능을 다루기 위해서는 시스템 서비스인 NotificationManager와 Notification객체를 사용한다.
1. Notification객체 생성
Notification클래스는 생성자에 상단 상태표시줄에 보여질 아이콘, 타이틀 문구, 시간을 인자로 넘겨준다.
Notification notify = new Notification(R.drawable.my_icon, "Notification!", System.currentTimeMillis();

2. Intent객체 생성
상태표시줄을 끌어내려 확장했을때 알림을 선택할 경우 다른 activity를 실행하도록 설정한다.
Intent goto = new Intent(my_notification.this, my_notification.class);
3. PendingIntent객체 생성
PendingIntent intentBack = PendingIntent.getActivity(Notifications.this, 0, toLaunch, 0);
4. 생성한 Notification객체의 setLatestEventInfo()메서드 실행
상태표시줄을 끌어내려 확장했을때 표시될 타이틀, 내용, 선택시 실행될 intent객체 지정
notify.setLatestEventInfo(ctx, "Title", "Text-1234567890", intentBack);

5. NotificationManager 인스턴스 생성
NotificationManager notifier = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6. NotificationManager의 인스턴스의 notify()메서드로 Notification객체 전달
각 알림을 구별할 수 있도록 id와 Notification객체 전달
notifier.notify(0x1001, notify);
* 같은 Notification객체로 중복된 알림이 있을 경우 상태표시줄에 알림 횟수를 표시해줄 수 있다.
notify.number = 2;

* 알림의 삭제
알림을 삭제하기 위해서는 알림의 id를 지정하여 삭제할 수 있다.
notifier.cancel(0x1001);
또한, Nofitication객체의 flags속성에 FLAG_AUTO_CANCEL을 지정해 줄 경우 사용자가 해당 알림을 선택시 즉시 삭제된다.
notify.flags |= Notification.FLAG_AUTO_CANCEL;
* 알림의 사용자 피드백
- 진동(Vibrator)
Vibrator를 사용하기 위해서는 AndroidManifest.xml에 아래 권한을 추가해줘야 한다.
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
진동의 설정은 홀수 배열은 진동의 시간, 짝수 배열은 진동이 멈춰있는 시간이다.
다음은 100ms진동 후 100ms진동이 멈추고, 이후 200ms간 진동이 울리고, 200ms동안 진동이 멈추게 된다.
notify.vibrate = new long[] {100, 100, 200, 200, 300, 300, 400, 400, 500, 500};
이 패턴을 잘 활용하면 사용자에게 다양한 피드백을 전달할 수 있다.
FLAG_INSISTENT플래그를 설정할 경우 자용자가 알림을 제거할때까지 반복 재생된다.
- 지시등(LED)
LED는 Vibrator와 달리 LED를 켜고 끄는 시간만 지정해주고 사용자가 알림을 제거할때까지 반복된다.
notify.flags |= Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = Color.GREEN;
notify.ledOffMS = 1000;
notify.ledOnMS = 1000;
- 소리(Sound)
notify.sound = Uri.parse("file:/system/media/audio/ringtones/sample.ogg");
FLAG_INSISTENT플래그를 설정할 경우 자용자가 알림을 제거할때까지 반복 재생된다.
* 사용자 정의 알림 만들기
사용자는 자신만의 알림화면을 구성할 수 있다.
알림에 표시될 별도의 layout을 생성한 후 아래와 같이 지정하면 된다.
RemoteViews remote = new RemoteViews(getPackageName(), R.layout.mynoti);
remote.setTextViewText(R.id.txxTitle, "Title");
remote.setTextViewText(R.id.txxText, "Text-1234567890");
notify.contentView = remote;

반응형
'Android > Tip&Tech' 카테고리의 다른 글
MySQL과 SQLite 날짜 함수 비교 MySQL (0) | 2011.01.13 |
---|---|
안드로이드 네트워크 상태 확인!! (0) | 2011.01.12 |
xml에 color설정하는 법 (0) | 2011.01.05 |
Translucent (0) | 2011.01.04 |
1. SurfaceView는 무엇인가? (3) | 2011.01.04 |