์๋ ํ์ธ์! ์ด๋ฒ์๋ ์๋๋ก์ด๋ ์ฑ์์ ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค์ ์๋ฆผ(Notification)์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์๋๋ก์ด๋์์๋ ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค(Foreground Service)๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์์๊ฒ ์ฑ์ด ์คํ ์ค์์ ์๋ฆฌ๊ณ , ์ฑ์ด ๋ฐฑ๊ทธ๋ผ์ด๋์์๋ ๊ณ์ํด์ ๋์ํ๋๋ก ํ ์ ์์ต๋๋ค.
๋ํ ์๋ฆผ(Notification)์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์์๊ฒ ์ ๋ณด๋ ์ด๋ฒคํธ ๋ฑ์ ์๋ฆด ์ ์์ต๋๋ค.
1.
์ฐ์ , ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ AndroidManifest.xml ํ์ผ์ "android.permission.FOREGROUND_SERVICE" ๊ถํ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2.
๋ค์์ผ๋ก๋ ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค๋ฅผ ์คํํ๊ธฐ ์ํด MainActivity.java ํ์ผ์์ NotificationCompat.Builder๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆผ์ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
private NotificationManager notificationManager;
private static final String NOTIFICATION_CHANNEL_ID = "channel_id";
private static final String NOTIFICATION_CHANNEL_NAME = "channel_name";
private static final int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_message))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(false)
.setOngoing(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
์ ์ฝ๋์์๋ ์๋ฆผ์ ๋ง๋ค๊ธฐ ์ํด NotificationCompat.Builder๋ฅผ ์ฌ์ฉํ๊ณ , ์์ฑํ NotificationChannel์ ์ฌ์ฉํ์ฌ ์๋ฆผ์ ํ์ํฉ๋๋ค.
์ด๋, setAutoCancel(false)๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆผ์ ์ญ์ ํ์ง ์๋๋ก ์ค์ ํ๊ณ , setOngoing(true)๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ฐ์ ๊ณ์ํด์ ์๋ฆผ์ด ํ์๋๋๋ก ํฉ๋๋ค.
3.
๋ํ onDestroy() ๋ฉ์๋์์๋ ์ฑ์ด ์ข ๋ฃ๋ ๋ ์๋ฆผ์ ์ญ์ ํฉ๋๋ค.
@Override
public void onDestroy() {
super.onDestroy();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
4.
๋ง์ง๋ง์ผ๋ก, NotificationChannel์ ์์ฑํ๋ ๋ฉ์๋์ธ createNotificaionChannel()์ ์์ฑํฉ๋๋ค.
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
notificationChannel.setShowBadge(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}
}
์ ์ฝ๋์์๋ "NotificationChannel" ํด๋์ค์ ์์ฑ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ์๋ฆผ ์ฑ๋์ ๋ง๋ค๊ณ ,
"setShowBadge()" ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฑ์ง ์์ด์ฝ์ ์จ๊ธฐ๊ณ
"setLocksereenVisibility()" ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆผ์ด ์ ๊ธ ํ๋ฉด์์ ๋ณด์ด๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
5.
๋ง์ง๋ง์ผ๋ก "NotificationManager"์ "createNotificationChannel()" ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์๋ก ๋ง๋ ์๋ฆผ ์ฑ๋์ ์์คํ ์ ๋ฑ๋กํฉ๋๋ค.
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
notificationChannel.setShowBadge(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}
}
"NotificationManager.IMPORTANCE_LOW"๋ ์๋ฆผ์ ์ค์๋๋ฅผ ๋ํ๋ด๋ฉฐ, ์ด ๊ฒฝ์ฐ "์ค์๋๊ฐ ๋ฎ์"์ ๋ํ๋ ๋๋ค.
๋ ๋์ ์ค์๋๋ฅผ ๊ฐ์ง ์๋์ ๋ ๋์ ์ฐ์ ์์๋ก ํ์๋๋ฉฐ, ์ฌ์ฉ์์๊ฒ ๋ ๋ง์ ์ฃผ์๋ฅผ ์๊ตฌํฉ๋๋ค.