TestServiceの全体的なリファクタリング #28

Merged
asura146 merged 73 commits from improve/refactor_service into main 2024-07-12 02:17:26 +00:00
Showing only changes of commit 48c3bec004 - Show all commits

View File

@ -24,6 +24,7 @@ import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
@ -42,7 +43,6 @@ public class TestService extends Service {
// ユーザーID // ユーザーID
private String userId = null; private String userId = null;
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
//共有プリファレンス全体の準備 //共有プリファレンス全体の準備
@ -88,6 +88,15 @@ public class TestService extends Service {
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
} }
/**
* 通知が許可がされているかどうかを確認
* @return 通知の許可の有無 true: 許可されている false: 許可されていない
*/
private boolean isNotificationEnabled() {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
return notificationManagerCompat.areNotificationsEnabled();
}
private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理 private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理
// PeriodicTaskManagerのインスタンス化 // PeriodicTaskManagerのインスタンス化
@ -143,6 +152,9 @@ public class TestService extends Service {
public void Notification(Context context) {//実際に通知を行うメソッド public void Notification(Context context) {//実際に通知を行うメソッド
// 権限の保有を確認
if (!isNotificationEnabled()) return;
vibrateDevice(); vibrateDevice();
@SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) @SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
@ -156,17 +168,13 @@ public class TestService extends Service {
NotificationManager notificationManager = context.getSystemService(NotificationManager.class); NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
return;
}
notificationManager.notify(R.string.app_name, builder.build());//通知の表示 notificationManager.notify(R.string.app_name, builder.build());//通知の表示
} }
public void NotificationBluetooth(Context context) {//実際に通知を行うメソッド public void NotificationBluetooth(Context context) {//実際に通知を行うメソッド
// 権限の保有を確認 // 権限の保有を確認
if (ActivityCompat.checkSelfPermission(this, if (!isNotificationEnabled()) return;
android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) return;
vibrateDevice(); vibrateDevice();