[컴][안드로이드] Notification 정리

notification / notificationcompat / status bar / notification 사용법 / custom notification / notification customize





Notification

Notification 과 관련해서는 아래의 문서가 가장 자세히 나온 듯 하다. example 도 같이 보여주며서 설명 해 준다. 문서의 목차는 아래와 같다.

Notifying the User | Android Developers

  1. Building a Notification
    1. Create a Notification Builder
    2. Define the Notification's Action
    3. Set the Notification's Click Behavior
    4. Issue the Notification
  2. Preserving Navigation when Starting an Activity
    1. Set up a regular activity PendingIntent
    2. Set up a special activity PendingIntent
  3. Updating Notifications
    1. Modify a Notification
    2. Remove Notifications
  4. Using Big View Styles
    1. Set Up the Notification to Launch a New Activity
    2. Construct the Big View
  5. Displaying Progress in a Notification
    1. Display a Fixed-duration progress Indicator
    2. Display a Continuing Activity Indicator


그밖에...

4.1 이전의 status bar 에 대한 설명 : Status Notifications | Android Developers


Notification 이미지



Image from Notifications | Android Developers


  1. Content title
  2. Large icon(Large icon size)
  3. Content text
  4. Content info
  5. Small icon
  6. Time
  7. Details area
    1. Big picture style
      The details area contains a bitmap up to 256 dp tall in its detail section.
    2. Big text style
      Displays a large text block in the details section.
    3. Inbox style
      Displays lines of text in the details section.

Big view 스타일(Big picture style, Big text Style, Inbox style) 에서만 가능한 기능

  • Big content title
  • Summary text



반드시 설정해야 하는 부분(Required notification contents)

  1. A small icon, set by setSmallIcon()
  2. A title, set by setContentTitle()
  3. Detail text, set by setContentText()

Example


Notification noti = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.stat_sample)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.stat_sample))
                .setContentInfo(getText(R.string.mp3player_content_info))
                .setContentIntent(contentIntent)
                .build();


// Send the notification.
((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION, noti);




NotificationCompat


Compatibility

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Compatibility

하위 호환성을 위해 NotificationCompat 를 제공한다.


Example


NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
        .addLine(
                "M.Twain (Google+) Haiku is more than a cert...")
        .addLine("M.Twain Reminder")
        .addLine("M.Twain Lunch?")
        .addLine("M.Twain Revised Specs")
        .addLine("M.Twain ")
        .addLine(
                "Google Play Celebrate 25 billion apps with Goo..")
        .addLine(
                "Stack Exchange StackOverflow weekly Newsl...")
        .setBigContentTitle("6 new message")
        .setSummaryText("mtwain@android.com");

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.stat_sample)
        .setContentTitle(title)
        .setContentText(text)
        .setStyle(style);

Notification noti = mBuilder.build();
noti.flags |= Notification.FLAG_NO_CLEAR;

// Send the notification.
((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION, noti);






Action


Example

Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);



NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.stat_sample)
        .setContentTitle("Content Title")
        .setContentText("Contetn Text")
        .addAction(R.drawable.stat_sample, "added action", pIntent)
        .setContentIntent(pIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());


addAction()

addAction 을 하면 버튼이 추가된다. 이 버튼을 눌렀을 때의 동작을 set 해준다.

setContentIntent()

setContentIntent() 는 notification panel 에서 notification 을 click 했을 때 실행된다.



Customization

RemoteViews

자신이 원하는 모양으로 notification 을 보여주고 싶다면 RemoteViews 를 이용하자. 이 녀석을 new 해서 Notification 의 bigContentView 나 contentView 에 assign 해주면 된다.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.stat_sample)
                .setContentTitle(title)
                .setContentText(text)
                .setContentIntent(contentIntent);

Notification noti = mBuilder.build();

RemoteViews expandedView = new RemoteViews(this.getPackageName(), R.layout.mp3player_notification);
expandedView.setOnClickPendingIntent(R.id.noti_image, contentIntent);
expandedView.setOnClickPendingIntent(R.id.noti_button_play, contentIntent);

noti.bigContentView = expandedView;


mBuilder.setContentIntent() 는 notification 전체에 대한 것이고,
setOnclickPendingIntent() 는 각각의 View 에 지정해 줄 수 있다.


Example





References

  1. Notifying the User | Android Developers




댓글 없음:

댓글 쓰기