I should receive a notification when the data in the Firebase real-time data base is changed.
The notification should only be given when the app is not in use. It should not give a notification when the app is being used or opened.
I have created a new class MyFirebaseMsgService
for My project. and this class contains this code.
I have added the needed manifest and the dependencies in build gradle app.
i didn't receive any notifications and the i didn't receive any logs in logcat about the firebase messaging.
but if I create a new camping in the firebase console it is working.
added the screenshots below
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private DatabaseReference mDatabase;
@Override
public void onCreate() {
super.onCreate();
mDatabase = FirebaseDatabase.getInstance("XXXXx my url XXXXXX").getReference();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// Get the value of the "attribute" key from the data payload
String attributeValue = remoteMessage.getData().get("attribute");
// Check if the app is not running or being used
if (!isAppInForeground()) {
// Send notification only if the attribute value has changed
mDatabase.child("data").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String currentValue = dataSnapshot.getValue(String.class);
if (currentValue != null && !currentValue.equals(attributeValue)) {
sendNotification();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
}
}
}
public boolean isAppInForeground() {
// Check if the app is running in the foreground
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();
if (runningProcesses != null) {
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.processName.equals(getPackageName()) && processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
}
return false;
}
public void sendNotification() {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notlogo)
.setContentTitle("hi")
.setContentText("test")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, notificationBuilder.build());
}
}
}
There is no way to reliably do this in just your Android application, as the OS actively works against apps keeping an open connection like that.
You'll need to use something like Cloud Functions, where this is in fact the first use-case in the documentation: Notify users when something interesting happens
This has been covered before, so also check out: