I haven't my own server and client doesn't want a server. Can i do it with direbase notifications? And how i will calculate how amny days spent from last user session?
4 Answers
--------------------------------NEW ANSWER------------------------------------
You should be able to do this using the Firebase Console. When creating a Cloud Message you can select in the "Target" section "Last App Engagement" and select that you want the message to go to everyone that hasn't engaged with the app for say 1 day.
After that you set in Scheduling that you want it to be a recurring campaign and you set it to happen "Daily" at say 12pm and set that you want each user to get this only once. Now you have a campaign that every day at 12pm checks who wasn't in the App for 1d and sends him the campaign, but only sends it once to not spam the same guy every single day.
--------------------------------OLD ANSWER -----------------------------------
You could create for example a DailyJob within your app and that DailyJob could either on it's own check how many days since last session was active and create a local notification or you could use that DailyJob to send to say Firebase a custom attribute "days_since_last_login" and setup different campaigns for 2 days offline, 7 days offline, 31 days offline and so on in Firebase.
To easily create a DailyJob you can use Evernote Android Job library.

- 460
- 3
- 9
- You can use firebase to make push notification.
- In this case, you don't have any server to make push notification and track the last user session, I think you can follow this:
- Use local notification (not push notification) to notify user.
- Store user session to Shared Preferences.
- Create a background service to trigger local notification base on the last session stored. But you should have a clear view of notification on Android to understand how it works first.

- 136
- 2
-
Background Services are not allowed in newer Android APIs, to do something like this you would need to setup a DailyJob or setup time based Jobs, but I think a DailyJob that runs in the afternoon is a good solution for this problem. – Rafael Skubisz Nov 06 '18 at 10:50
-
Background service is still there but it has some limitations on Android 8.0 and higher. Anw, create a scheduler job is a good idea :) – Louis Solo Nov 06 '18 at 11:02
-
Background Services are still there, but apps that are not in the foreground are not allowed to start background services. There are some small exceptions like receiving a notification pending intent or receiving a High Priority FCM Notification allows you to start a background service but only for a limited time. With Android P they are limiting this even farther by introducing App Buckets and even High Priority Notifications won't help you if your App isn't in the right App Bucket. I personally stay away from services after Android O, cause they change with every release. :P – Rafael Skubisz Nov 06 '18 at 11:24
This is simple: don't use Firebase or the internet at all.
This is what you want to do: every time the user logs into your app, Create an Alarm for 48 hours (2 days) from when onResume
and cancel any previous alarms. Or cancel previous alarms during onResume
and create a new one in onPause
.
Then, setup the alarm receiver to send a local notification. AlarmManager with Notification Android
Every time the user opens your app, it pushes the local notification back. When they go 2 days without using it, they will get a notification at around the same time that they used it previously.

- 2,320
- 1
- 13
- 20
"Can I do it with firebase notifications?"
Yes, you can with cloud function code.
Just trigger an event after a new notification added to the database.
My below code trigger an event after a new notification added to DB and looks for a post older than 24hrs. You can use it and change it for 48hrs
The code work below:
Trigger event after data is added to DB.
Check the database for old notifications older than 24hrs.
Retrieve them and delete (by changing their value to null);
//DELETE OLD NOTIFICATION LESS THAN 24HRS ///////////////// exports.deleteOldItems = functions.database.ref('/notification/{user_id}/{notification_id}').onWrite((change) => {
const ref = change.after.ref.parent; // reference to the parent const yesterday = Date.now() - 86400000; const oldItemsQuery = ref.orderByChild('time').endAt(yesterday); return oldItemsQuery.once('value').then((snapshot) => { // create a map with all children that need to be removed const updates = {}; snapshot.forEach(child => { updates[child.key] = null; }); return ref.update(updates); // execute all updates in one go and return the result to end the function });
});
////////////////////DELETE FUNCTION ENDED HERE//////////////////////
The most important part is how to calculate for 48hrs (which is 2 days)
PS: this is still 24hrs. I will update the answer when finding a way to convert for 48hrs.
const yesterday = Date.now() - 86400000;
EDIT:172800000
is for 2 days ago (24hrs)
Now, you subtract the current date from 2 days ago and compare it with the notification date.
const yesterday = Date.now() - 172800000;

- 7,953
- 5
- 49
- 80

- 972
- 1
- 13
- 26