通知を飛ばすためのパーツをMainActivityに追加しました。

This commit is contained in:
磯野 2023-12-22 12:00:58 +09:00
parent 17b3106bdd
commit 737b2c8a4d
2 changed files with 45 additions and 0 deletions

View File

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -1,8 +1,16 @@
package com.example.childguard;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Vibrator;
import com.google.android.material.bottomnavigation.BottomNavigationView;
@ -12,6 +20,16 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通知のやつ
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "サンプルアプリ", importance);
channel.setDescription("説明・説明 ここに通知の説明を書くことができる");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
//通知のやつ
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
@ -34,4 +52,29 @@ public class MainActivity extends AppCompatActivity {
});
}
//通知のやつ
public void notifyTest() {
((Vibrator)getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(this, "CHANNEL_ID")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setContentTitle("タイトル")
.setContentText("メッセージ・メッセージ")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
notificationManager.notify(R.string.app_name, builder.build());
}
}