본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] how can I detect if notification clear button is clicked?

by 돼지왕 왕돼지 2013. 10. 16.
반응형


 android, how can I detect if notification clear button is clicked?

 

[android] how can I detect if notification clear button is clicked?


[En]


Very easy.

When you make Notification instance, just add delete intent with setDeleteIntent() method.


private Notification getNotification( Context context, Intent intent, String pushMessage ){

int notiCount = Application.getNotiMessageCount();

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

.setSmallIcon( R.drawable.ic_launcher )

.setContentTitle(getString(R.string.app_name))

.setContentText( pushMessage )

.setContentInfo( Integer.toString( notiCount ) )

.setNumber( notiCount )

.setAutoCancel( true )

.setContentIntent( getPendingIntent( context, intent ) )

.setDeleteIntent( getDeletePendingIntent( context ) );

return builder.getNotification();

}


private PendingIntent getPendingIntent( Context context, Intent intent ){

Intent pushIntent = new Intent( context, MainActivity.class );

return PendingIntent.getActivity( context, 0, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT );

}


private PendingIntent getDeletePendingIntent( Context context ){

Intent deleteIntent = new Intent( context, NotificationDeleteBroadcastReceiver.class );

return PendingIntent.getBroadcast( context, 0, deleteIntent, 0 );

}


If you code like above, NotificationDeleteBroadcastReceiver receives intent when the user pressed "clear" button.

Don't forget to register your broadcast receiver to the manifest!!







[Kr]


간단하다.

Notification 인스턴스를 만들 때 setDeleteIntent() 를 이용하여 delete intent 를 넣어주기만 하면 된다.



private Notification getNotification( Context context, Intent intent, String pushMessage ){

int notiCount = Application.getNotiMessageCount();

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

.setSmallIcon( R.drawable.ic_launcher )

.setContentTitle(getString(R.string.app_name))

.setContentText( pushMessage )

.setContentInfo( Integer.toString( notiCount ) )

.setNumber( notiCount )

.setAutoCancel( true )

.setContentIntent( getPendingIntent( context, intent ) )

.setDeleteIntent( getDeletePendingIntent( context ) );

return builder.getNotification();

}


private PendingIntent getPendingIntent( Context context, Intent intent ){

Intent pushIntent = new Intent( context, MainActivity.class );

return PendingIntent.getActivity( context, 0, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT );

}


private PendingIntent getDeletePendingIntent( Context context ){

Intent deleteIntent = new Intent( context, NotificationDeleteBroadcastReceiver.class );

return PendingIntent.getBroadcast( context, 0, deleteIntent, 0 );

}


위와 같이 코딩하면, NotificationDeleteBroadcastReceiver 에서 유저가 "Clear(지우기)" 버튼을 눌렀을 때 intent 를 받을 수 있다.

물론 manifest 에 broadcast receiver 를 등록하는 것을 잊지 말자.






반응형

댓글