본문 바로가기

ANDROID의 속삭임

[android][Notification]통지 등록및 통지유틸


* 4.2 미만 버전에서 사용 하는 방법.

@SuppressWarnings("deprecation")

public void onBtnClick(View view) {

NotificationManager notificationManager = (NotificationManager) 

getSystemService(NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher,

"알려드립니다", System.currentTimeMillis());

// 이용자가 통지를 확인한 후에는 통지를 표시하지 않도록 설정

notification.flags = Notification.FLAG_AUTO_CANCEL;


Intent intent = new Intent(this, NotificationFollower.class);

PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);

notification.setLatestEventInfo(this, "우편물 도착",

"소포가 배달되어 관리실에 있습니다", activity);

notification.number += 1;

notificationManager.notify(0, notification);

}


* 4.2이상 버전에서 사용하는 방법.

public void onBtnClick(View view) {

// Prepare intent which is triggered if the

// notification is selected


Intent intent = new Intent(this, NotificationFollower.class);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);


// Build notification

// Actions are just fake

Notification noti = new Notification.Builder(this)

       .setContentTitle("New mail from " + "test@gmail.com")

       .setContentText("Subject")

       .setSmallIcon(R.drawable.ic_launcher )

       .setContentIntent(pIntent)

       .addAction(R.drawable.ic_launcher , "Call", pIntent)

       .addAction(R.drawable.ic_launcher , "More", pIntent)

       .addAction(R.drawable.ic_launcher , "And more", pIntent).build();

   

 

NotificationManager notificationManager = 

 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


// Hide the notification after its selected

noti.flags |= Notification.FLAG_AUTO_CANCEL;


notificationManager.notify(0, noti); 

}






* NotifiUtil.java

package com.example.notification;


import java.util.ArrayList;


import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;


public class NotifiUtil {

/**

* 통지를 등록한다. 

* @param context - 호출하는 Activity 자신

* @param intent - 등록된 통지를 클릭했을 때, 구동될 Activity 정보

* @param noticeId - 통지에 부여할 고유번호

* @param iconId - 통지에 표시될 아이콩의 리소스 아이디

* @param ticker - 통지가 등록될 시에 안테나 영역에 일시적으로 보여질 메시지

* @param title - 통지 영역에 표시될 제목

* @param message - 통지 영역에 보여질 내용

*/

public static void add(Context context, Intent intent, int noticeId, int iconId, String ticker, String title, String message){

//통지 객체 생성 

//(표시 아이콘, 티거메세지, 현재 시간)

Notification noti = new Notification(iconId, ticker, System.currentTimeMillis());

// 통지를 선택 하였을때, 실행시킬 Activity를 설정 하기위해 PendingIntent를 생성

PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);

// 통지의 상세 정보 지정

// (출처 Aactivity, 제목, 내용, 선택시 구동할 App정보)

noti.setLatestEventInfo(context, title, message, pintent);

// 시스템에 통지 정보를 등록

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

nm.cancel(noticeId);

nm.notify(noticeId, noti);

}

/**

* 통지를 해제한다. 

* @param context - 호출하는 Activity 자신

* @param noticeId - 통지에 부여할 고유번호

*/

public static void remove(Context context, int noticeId){

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

nm.cancel(noticeId);

}

/**

* 모든 통지 해제

* @param context

*/

public static void removeAll(Context context){

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

nm.cancelAll();

}

}