Notifications in Ionic 2 - android

I have gone through many posts.
Its bit confusing to understand that does ionic 2 app send local-notification even if my-app-is-killed-by-user or not ?
I have below scenario.
Assuming that app-is-killed-by-user
Can we run the app in background (like after 24 hours check if some
conditions are true by manipulating local/cloud storage) then send local notification ?
I am new to ionic 2. So can anyone let me know that above scenario is possible ?
If not possible then let me know that can we manipulate (get and analyse) local-storage via push notification server (assuming that app-is-killed-by-user) ?

Both are possible. I'll tell you how i would do them and not share code (it can be long), i'll give you the steps and you can figure it out.
For the first scenario you'll need LocalNotification plugin, LocalStorage, Platform import from 'ionic-angular' and MomentJS(not required, but highly recommended).
You'll need to create a service, at least it's good so you don't need to put everything in your app.components.
You can use 3 of the Platform methods to verify and set the LocalNotification, they're .pause, .resume and .ready.
Pause will check if the user has left the app, but it's still running on background.
Resume will check if the user has come back to the app, but'll not trigger if the app was killed.
Ready will trigger when platform's ready, you'll use this to check if the app was killed and is starting for "the first time".
You'll need MomentJS to manipulate time, since it's easier to add days/hours to your current datetime.
LocalNotification accepts both ISO string and UNIX timestamp to schedule a notification.
Inside your created service you'll have methods to schedule and cancel a LocalNotification, check the plugin wiki for those.
On the pause method you'll call the schedule method and schedule a localNotification having Moment add 1 day to your current time: Moment().add(1, 'day').unix() and use the result in the 'at' of your schedule.
This'll return a promise with the data of your notification and it's id, save this id in your localStorage like this.storage.set('Daily', ReturnedID);
When the app is ready or resume you'll cancel the notification.
So basically that's it, when going off you save a notification, when going in you cancel it.
The second scenario is a little bit harder and you'll need a back-end for this since you'll need to keep checking the time that user has gone off the app. You'll need only MomentJS, the Platform import, you push service plugin and a way to connect to your database (via Http or plugin, if it's a service like Firebase).
The steps are the same for the first scenario, the new service, the platform calling this service methods, but now will add some methods for you push plugin, so when you receive a push you can show and manipulate data.
Most push services (and localNotification) have a Data attribute where you can share some data with the user, in your case it'll be the data you want to safe in the localStorage.
You'll need a token of that user so you can send a push to him, see your push docs to see how you can get that token and save it to the user database register/node.
When the user leave the app you'll store in your database the time you want the user to receive that push (i'm assuming you want to do the same as the first scenario, but in a different way).
When the user come back to the app, you'll detele that time register.
The tricky part: you'll need your back-end to go through every user register and get the time they need to receive the push, so if the time is < the current time you'll send the push and delete that register.
In the app when the user receive the push a function'll save the data to localStorage, manipulate, ready or everything you want.
Sorry i can't share some code, but if you need furter explanation or ideas just ask.
Hope this helps :D

Related

Do I need an Android Service to check if something is changed in the DB?

i'm creating android app, and using Firebase.
I have 'Missions', and 'Mission' have status.
If the status changes to 'Active' - I need to 'Wake up' my app and run the code of 'ActiveMissionActivity'.
(The work of the service should be just 'Waking up the app' and go to another activity)
I could write missionsDbRed onChildChanged listener which will be activated when the status is active - but only when the app is running.
What is the good practice for this issue? My friend suggested me to use 'Android Service', but i'm not sure which type do I need, and if there is something that works well with the Firebase DB.
For example i've seen 'Firebase Cloud Functions' But i'm not sure which one is more suitable and why. Thanks
As push-notifications are not a solution to your issue, the best way of handling this matter is by building a service that checks (in the database) if the value you are interested in has changed.
Now regarding the service, a bad idea would be to run a service that continuously checks that value. This would result in a huge battery drain and you risk your process to be killed as android is freeing up resources. you can notice this here.
You can fix this by telling the service when to start checking the value you are interested in.(e.g. by using Alarm API and assuming you know when this should happen) You can see something similar here.
Keeping your service alive as less as possible is probably the safest way.
As for google cloud functions, you might wanna use those for sending notifications prior to the actions that happen through the service you established.
So if the status changes to active while your app is closed, so that means the value changed somewhere out of your app right? If that's the case simply send a push notification to users device to wake it up. It is a lot more performant than using a background service.

Firebase database backgroundservice error [duplicate]

I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.
On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?
firebaser here
Great question!
When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.
What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.
We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

Handling keepSynced() while on background on Android and with FCM

I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.
On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?
firebaser here
Great question!
When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.
What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.
We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

Approach to use:- Manual notification or using Push Notification via web service

As title suggest I want to know which approach will be best suited for my situation.Here is what I need.
In my app I am having a list of events. when clicking on any event from list, it will take user to event details page. On that page I have a Remind Me button on press of which user will be notified few hours/days before event. I want to know which of the two approach will be better.
First Approach
I register an alarm manually with the system which will get trigger according to the time which I will provide at the time of setting alarm.
Second Approach
I should use a web service along with push notification and when push is received show notification to the user.
the first approach is sufficient. A local notification is all you need in this case. Using the second approach is definitely overkill. The benefits of the first method are it will work offline and it save you cost to send a remote notification.
The first approach is the best. If you go with the web approach there can be chances that the user isn't using data-plan. And yes it will surely work for multiple instances. Instead of relying solely on alarms. You can use the calendar API and set a reminder. You can set multiple reminder with alarms that way.
I hope it helps.

Communication between the same app in different smartphones

I would like to develop an application. It could be a game or whatever. I would have the same application in two or more devices. When one of them finish his tasks the other "client" must receive an notify that he has task to do and his datas should be updated automatically with the last changes. I guess that I would need a server in the middle where I'd save the model with the datas and send to them where the smartphones are communicating through it. It could be like a cardgame or kind of.
So,,,,
1. Two or more clients with the same application.
2. When one of them finish his task or turn, the other client should get a notify with his dates updates.
I have been looking at GCM, but I don't know if I could send complex datas through it or not,, and maybe there is a better way to make these kind of things.
Could someone give a clue where I can start??
Thank you!.
In your architecture, you must separate out the control and data aspects of the app.
You don't need the cloud to initiate a push of the entire data. If your app on any particular device gets a notification that an update is pending, then it can initiate the download at its convenience. Just use the GCM to push a notification that some task is pending for the app.

Categories

Resources