๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป๊ฐœ๋ฐœ

[Java] ์•ˆ๋“œ๋กœ์ด๋“œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ: ํฌ๊ทธ๋ผ์šด๋“œ ์„œ๋น„์Šค์™€ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ์ฑ„๋„ ํ™œ์šฉ

by ๋ˆˆ๋ˆ„ :) 2023. 5. 8.

 

 

 

 

์•ˆ๋…•ํ•˜์„ธ์š”! ์ด๋ฒˆ์—๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์—์„œ ํฌ๊ทธ๋ผ์šด๋“œ ์„œ๋น„์Šค์™€ ์•Œ๋ฆผ(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"๋Š” ์•Œ๋ฆผ์˜ ์ค‘์š”๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ด ๊ฒฝ์šฐ "์ค‘์š”๋„๊ฐ€ ๋‚ฎ์Œ"์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๋” ๋†’์€ ์ค‘์š”๋„๋ฅผ ๊ฐ€์ง„ ์•Œ๋žŒ์€ ๋” ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋กœ ํ‘œ์‹œ๋˜๋ฉฐ, ์‚ฌ์šฉ์ž์—๊ฒŒ ๋” ๋งŽ์€ ์ฃผ์˜๋ฅผ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค.