I have a Push notification code that is activated when one of the data in my Firebase Databade changes. The code works perfectly when I am inside the Fragment where it is incorporated, but when I close the app or change Fragment or Activity it stops working ...
What could be the problem? ... When I send notifications from the Firebase platform there are no problems, the notifications arrive even though the application is closed, but when I work with the code within this Fragment it is not the same , it only works as long as the application is open and it is in the Fragment that contains the code.
This is the code that is implemented and that executes the notification when a value changes in the Database
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> items = dataSnapshot.getChildren().iterator();
entries.clear();
while (items.hasNext()) {
DataSnapshot item = items.next();
log = (int) dataSnapshot.getChildrenCount();
txtlogactual.setText(log + "");
int aviso5 = Integer.parseInt(Numero5) - 5;
int aviso1 = Integer.parseInt(Numero5) - 1;
if (getActivity() != null) {
new TextoParpadeante(getActivity(), txtlogactual);
if (aviso5 == log) {
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity(), NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.alerta)
.setTicker("Hearty365")
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Aviso")
.setContentText("¡Ingresa ahora!")
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
}
if (aviso1 == log) {
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity(), NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.alerta)
.setTicker("Hearty365")
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Aviso")
.setContentText("¡Ingresa ahora!")
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
}
}
What could be the problem?
I hope you can help me, of course, thank you very much for your time and help!