android-lecture

android lecture notes

View on GitHub

알림(Notification)

허준영(jyheo@hansung.ac.kr)

알림(Notification)

NotificationManagerCompat 와 알림 채널

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationManagerCompat = NotificationManagerCompat.from(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26
    mNotificationManagerCompat.createNotificationChannel(
            new NotificationChannel(CHANNEL_ID, "default channel",
                    NotificationManager.IMPORTANCE_DEFAULT));
} } ```

알림 생성

단순 알림 생성

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);

mBuilder.setSmallIcon(R.drawable.ic_alarm_on_black_24dp);
mBuilder.setContentTitle(getResources().getString(R.string.notif_title));
mBuilder.setContentText(getResources().getString(R.string.notif_body));

// MY_NOTIFICATION_ID allows you to update the notification later on.
mNotificationManagerCompat.notify(MY_NOTIFICATION_ID, mBuilder.build());

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 액티비티 연결하기

알림에 액티비티 연결하기 - 일반 액티비티

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 액티비티 연결하기 - 알림 전용 액티비티

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 버튼 추가

Intent callintent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234"));
requestID++; //unique requestID
flags = PendingIntent.FLAG_CANCEL_CURRENT;
pIntent = PendingIntent.getActivity(this, requestID, callintent, flags);
mBuilder.addAction(R.drawable.ic_phone_black_24dp, "Call", pIntent);

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 확장 뷰

NotificationCompat.BigTextStyle btStyle =
        new NotificationCompat.BigTextStyle().bigText(
             getResources().getString(R.string.long_notification_body));
mBuilder.setStyle(btStyle);

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 확장 뷰

https://github.com/jyheo/AndroidTutorial/tree/master/Notification

알림에 프로그래스 표시

bg right:15% w:200

https://github.com/jyheo/AndroidTutorial/tree/master/Notification