안드로이드 알람 생성 2가지 방법 (Android Notifications Tutorial with Examples)
프로그래밍/Android (Java)2022. 7. 18. 12:13
반응형
아래 예제는 Notification 두 가지 방법을 다루고 있습니다.
1. 진동 및 알림 메세지가 전면 상단에 출력되며, 상태바에서도 아이콘으로 나타나는 방법
2. 진동 및 알림 메세지 없이 맨 위 상태바에서만 조용히 아이콘으로 나타나는 방법
아래는 원문 출처를 따라하며 요약한 내용입니다.
[출처] https://o7planning.org/10427/android-notification
==================================================
1. Empty Activity 로 프로젝트를 생성합니다.
2. 알림 메세지에 사용할 이미지 파일 두개를 생성합니다.
drawable/icon_notify1.png
drawable/icon_notify1.png
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/editText_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:ems="10" android:hint="Title" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText_message" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:ems="10" android:hint="Message" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText_title" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="33dp" android:text="Send on Channel 1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText_message" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="23dp" android:text="Send On Channel 2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button1" /> </androidx.constraintlayout.widget.ConstraintLayout> |
4. NotificationApp.java
package org.o7planning.notificationbasicexample; import android.app.Application; import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.Build; public class NotificationApp extends Application { public static final String CHANNEL_1_ID = "channel1"; public static final String CHANNEL_2_ID = "channel2"; @Override public void onCreate() { super.onCreate(); this.createNotificationChannels(); } private void createNotificationChannels() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel1 = new NotificationChannel( CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH ); channel1.setDescription("This is channel 1"); NotificationChannel channel2 = new NotificationChannel( CHANNEL_2_ID, "Channel 2", NotificationManager.IMPORTANCE_LOW ); channel1.setDescription("This is channel 2"); NotificationManager manager = this.getSystemService(NotificationManager.class); manager.createNotificationChannel(channel1); manager.createNotificationChannel(channel2); } } } |
5. AndroidManifest.xml
<application android:name=".NotificationApp" .... > ... </application> |
6. MainActivity.java
package org.o7planning.notificationbasicexample; import android.app.Notification; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; public class MainActivity extends AppCompatActivity { private NotificationManagerCompat notificationManagerCompat; private EditText editTextTitle; private EditText editTextMessage; private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.editTextTitle = (EditText) this.findViewById(R.id.editText_title); this.editTextMessage = (EditText) this.findViewById(R.id.editText_message); this.button1 = (Button) this.findViewById(R.id.button1); this.button2 = (Button) this.findViewById(R.id.button2); this.button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sendOnChannel1( ); } }); this.button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sendOnChannel2( ); } }); // this.notificationManagerCompat = NotificationManagerCompat.from(this); } private void sendOnChannel1() { String title = this.editTextTitle.getText().toString(); String message = this.editTextMessage.getText().toString(); Notification notification = new NotificationCompat.Builder(this, NotificationApp.CHANNEL_1_ID) .setSmallIcon(R.drawable.icon_notify1) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .build(); int notificationId = 1; this.notificationManagerCompat.notify(notificationId, notification); } private void sendOnChannel2() { String title = this.editTextTitle.getText().toString(); String message = this.editTextMessage.getText().toString(); Notification notification = new NotificationCompat.Builder(this, NotificationApp.CHANNEL_2_ID) .setSmallIcon(R.drawable.icon_notify2) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_LOW) .setCategory(NotificationCompat.CATEGORY_PROMO) // Promotion. .build(); int notificationId = 2; this.notificationManagerCompat.notify(notificationId, notification); } } |
빌딩 이후 apk 파일을 설치, 어플을 실행하면 두 가지 예제의 알림 방식을 구분할 수 있습니다.
반응형
'프로그래밍 > Android (Java)' 카테고리의 다른 글
[Android] APNG 사용하기 (0) | 2023.08.24 |
---|---|
FCM 을 활용한 PUSH 메세지 보내기 (2024-11-23) (0) | 2022.10.19 |
안드로이드 비정상 종료 감지 처리 (어플 재시작) (0) | 2021.06.08 |
안드로이드 와이파이 SSID 가져오기 (0) | 2021.06.02 |
TextView 레이아웃 사이즈에 맞게 글자 크기 자동 조정하기 (0) | 2021.06.02 |
댓글()