権限チェックはメソッドの最初で行うように

This commit is contained in:
ろむねこ 2024-07-11 14:25:05 +09:00
parent 6667a63bff
commit e77b50eee3
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -161,6 +161,11 @@ public class TestService extends Service {
}
public void NotificationBluetooth(Context context) {//実際に通知を行うメソッド
// 権限の保有を確認
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) return;
final String CHANNEL_ID = "my_channel_id";
// 通知がクリックされたときに送信されるIntent
Intent intent = new Intent(context, MainActivity.class);
@ -196,10 +201,6 @@ public class TestService extends Service {
channel.enableVibration(true);
notificationManager.createNotificationChannel(channel);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
return;
}
notificationManager.notify(R.string.app_name, builder.build());//通知の表示
}