From 72b4a01018d8762432552206928da91d1f41a8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Thu, 11 Jul 2024 16:26:04 +0900 Subject: [PATCH] Debug --- .../com/example/childguard/TestService.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/example/childguard/TestService.java b/app/src/main/java/com/example/childguard/TestService.java index 7f488ee..e1bb01b 100644 --- a/app/src/main/java/com/example/childguard/TestService.java +++ b/app/src/main/java/com/example/childguard/TestService.java @@ -1,5 +1,6 @@ package com.example.childguard; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -51,6 +52,7 @@ public class TestService extends Service { public static final String TAG = "InspirationQuote"; private static final String CHANNEL_ID = "child_guard_emergency"; + private static final String BACKGROUND_CHANNEL_ID = "child_guard_background"; private static final int REQUEST_CODE = 100; // private static final int NOTIFICATION_DELAY = 5 * 60 * 1000; // 5 minutes // DEBUG @@ -70,19 +72,30 @@ public class TestService extends Service { return flags; // IDが初期化されていない場合は何もしない } + createRunningNotificationChannel(); + Intent notificationIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); + Notification notification = new NotificationCompat.Builder(this, BACKGROUND_CHANNEL_ID) + .setContentTitle("Test Service Running") + .setContentText("This service is running in the foreground") + .setSmallIcon(android.R.drawable.ic_menu_info_details) + .setContentIntent(pendingIntent) + .build(); + startForeground(1, notification); + setSnapshotListener(FirebaseFirestore.getInstance().document("status/" + this.userId)); if (isNotBluetoothGranted()) return flags; registerReceiver(receiver); - return flags; + return START_STICKY; } @Override public void onCreate() { super.onCreate(); if (!isNotificationChannelCreated()) { - createNotificationChannel(); + createAlertNotificationChannel(); } } @@ -98,7 +111,7 @@ public class TestService extends Service { /** * 通知チャネルの作成 */ - private void createNotificationChannel() { + private void createAlertNotificationChannel() { int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "通知", importance); channel.setDescription("第三者により置き去りの通報が行われたときに通知します。"); @@ -106,6 +119,18 @@ public class TestService extends Service { notificationManager.createNotificationChannel(channel); } + private void createRunningNotificationChannel() { + NotificationChannel serviceChannel = new NotificationChannel( + CHANNEL_ID, + "Foreground Service Channel", + NotificationManager.IMPORTANCE_DEFAULT + ); + NotificationManager manager = getSystemService(NotificationManager.class); + if (manager != null) { + manager.createNotificationChannel(serviceChannel); + } + } + /** * 通知が許可がされているかどうかを確認 * @return 通知の許可の有無 true: 許可されていない false: 許可されている