Я новичок в системах Android, и у меня есть следующий код, который использует FireBase
Сервисы, правильно у меня есть FirebaseMessagingService
, который при получении отправляет уведомление на телефон пользователя.То, что я хочу сделать, это у меня есть SQL
Таблица http://prntscr.com/mq57xh,, и я хочу сделать запрос в этой таблице SQL
и проверить, является ли это истиной или ложью, и если истина, то выпускает уведомление, еслинет, нет уведомления
Есть ли способ сделать это?
У меня есть следующий код на FireBaseMessagingService
public class FcmMessagingService extends FirebaseMessagingService {
//1
public Uri alarmSound;
public NotificationManager notificationManager;
//2
public String CHANNEL_1_ID = "channel1";
public String CHANNEL_1_DESCRIPTION = "Notificacoes gerais";
public NotificationChannel channel;
public String classs = "com.mysql.jdbc.Driver";
public String url = "jdbc:mysql://192.168.1.2:3306/test";
public String username = "root";
public String password = "password";
public Connection conn = null;
//3
public Statement stmt = null;
public int value;
public int valueid;
public boolean RequestValue = true;
public String valuestring;
public String notificationPrefix;
public String ip;
public String id = "";
public WifiManager wm;
public ResultSet rs;
public List<ActivityManager.RunningTaskInfo> taskInfo;
public ComponentName componentInfo;
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/notification");
wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
//2
CONN();
ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationschannel();
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("TAG", "Mensagem recibida");
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
if (conn != null)
{
Setupiduser();
Setupmessageprefix();
Log.e("TAG", "FCM SQL Database connected with success! IP: "+ ip);
} else
{
Log.e("TAG", "FCM SQL Database error on connection!");
}
if (isAppIsInBackground(this))
{
String localrow = message.replace(notificationPrefix, "");
if (isResponsible(localrow) == true)
{
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_1_ID);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.drawable.helponpointamarelo);
notificationBuilder.setAutoCancel(true);
//1
notificationBuilder.setSound(alarmSound);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(0,notificationBuilder.build());
} else if (isResponsible(localrow) == false)
{
notificationManager.cancelAll();
}
}
super.onMessageReceived(remoteMessage);
}
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(classs);
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException se) {
Log.e("Error SQL GetMessage" + url + username + password, se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("Error Exception", e.getMessage());
} catch (Exception e) {
Log.e("Error Exception", e.getMessage());
}
return conn;
}
public boolean isResponsible(String corredorname) {
valueid = (int) Integer.parseInt(id);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT responsible1 FROM t_users WHERE corredor ='" + corredorname +"'");
while (rs.next()) {
value = (int) rs.getInt("responsible1");
if (value == 0) {
RequestValue = true;
} else {
valuestring = "" + value;
if (valuestring.equals(id)){
RequestValue = true;
System.out.println("Es responsável por este corredor! (1)");
} else {
RequestValue = false;
}
}
}
} catch (SQLException asd) {
System.out.println("SQLException: " + asd.getMessage());
System.out.println("SQLState: " + asd.getSQLState());
System.out.println("VendorError: " + asd.getErrorCode());
}
return RequestValue;
}
Что происходит здесь,"if (isResponsible (localrow) == true)", похоже, не работает, кажется, что его даже не существует, и уведомление продолжает появляться
Есть какие-либо ответы по этому вопросу?
Спасибо!