通知を送る機能を追加しました。

This commit is contained in:
磯野 2024-01-15 16:41:09 +09:00
parent 8a7ab0b7a1
commit 7d26985334
3 changed files with 59 additions and 16 deletions

View File

@ -1,6 +1,7 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
} }
android { android {
@ -58,6 +59,7 @@ dependencies {
implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3' implementation 'androidx.compose.material3:material3'
implementation 'com.google.firebase:firebase-firestore:24.4.1'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'

View File

@ -24,11 +24,25 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager; BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter; BluetoothAdapter bluetoothAdapter;
public static final String TAG = "InspirationQuote";
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/q6t702C8nsXyehckByrr");//現在の位置を取得
boolean flg = false;
//日付を取得するやつ //日付を取得するやつ
public static String getNowDate() { public static String getNowDate() {
@SuppressLint("SimpleDateFormat") final DateFormat df = new SimpleDateFormat("yyy/MM/dd HH:mm:ss"); @SuppressLint("SimpleDateFormat") final DateFormat df = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
@ -40,23 +54,13 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
super.onStart();
//通知のやつ
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
//説明説明 ここに通知の説明を書くことができる
channel.setDescription("第3者からの通報を検知しました");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
//通知のやつ
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(v -> { bottomNavigationView.setOnNavigationItemSelectedListener(v ->
{
if (v.getItemId() == findViewById(R.id.navigation_home).getId()) { if (v.getItemId() == findViewById(R.id.navigation_home).getId()) {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(findViewById(R.id.fragmentContainerView).getId(), HomeFragment.newInstance("test", "tset")) .replace(findViewById(R.id.fragmentContainerView).getId(), HomeFragment.newInstance("test", "tset"))
@ -73,6 +77,39 @@ public class MainActivity extends AppCompatActivity {
return true; return true;
});
mDocRef.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
Log.d("nt","イベント開始");
if (flg && documentSnapshot != null && documentSnapshot.exists()) {
String parent = documentSnapshot.getString("parent");
Log.d("nt","レスポンスを検知しました1");
if (parent.equals("s")) {
//通知のやつ
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
//説明説明 ここに通知の説明を書くことができる
channel.setDescription("第3者からの通報を検知しました");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
//通知のやつ
Log.d("nt","レスポンスを検知しました2");
notifyMain();
} else if (e != null) {
Log.w(TAG, "Got an exceptiion!", e);
}
}
flg = true;
}
}); });
} }

View File

@ -1,4 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.14'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '8.0.2' apply false id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false