How can I send notifications if my app is closed? - android

How can I send my user notifications during the day if my app is closed? It should also work if he hasn't opened the app after a reboot of his phone. How can I implement something like that in my app and what things should I learn for that?
Thanks!

Push Notifications come through GCM (google cloud messaging) on Android. In order to see a tutorial on this you can go to https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/
As per "it should also work if he hasn't opened the app after a reboot of his phone" part, once you register your app with the client, you can employ logic of what happens (or doesn't happen) when you logout of the app. For example, you can unregister from the service so you don't get any notifications.
In your case, if you'd like to keep receiving notifications then you can do so by making sure you keep your token alive, preferably in System Preferences, and handle the case of what happens if the token needs to be updated. (see onTokenRefresh() callback)
First do the happy path and see you can register and receive, and then go and do any additional logic for your notifications.

You can use the AlarmManager for this. It will wake your Application with an scheduled Intent where you can perform your code, e.g. show the notification.
seems to be a duplicate to How to schedule a task using Alarm Manager

Related

Open an app which is not in background using FCM

My project is based on jitsi meet for android. I'm planning to go with react-native and firebase. The requirement is if one person calls the other person they will receive a call screen with ringtone. How can I achieve this if the app is not running in background?
This is a very tricky solution that you're trying to implement, especially it's working will vary a lot when it comes to deploying the application on Chinese OEM apps.
The process that you could instead follow is, Listen for FCM notifications along with that attach a payload to validate what kind of push notification is it. Based on that if it's a push notification for an incoming call, you can launch a foreground service which will allow your app to stay active and at the same time use a custom Broadcast Receiver. The Broadcast Receiver will receiver a trigger from your FCM Service and that will be used to open an activity that has your call screen UI.
Feel free to connect for a any help needed.
Using FCM, if there's a push notification, app automatically opens even if its not in background. But i believe you need to pass url on click of push notification of which triggers Deeplinking to actually trigger that page when the app opens, so directly it would navigate to the jitsi call page.
Check this link rn - deep link
Hope it helps. feel free for doubts

Short interval alarms in Android

I know that we can't set repeating alarms of short intervals in Android because it drains battery and has other such effects. Well, technically you can, but android will automatically push it up to a minute citing "suspiciously short duration".
So how exactly does an app like WhatsApp or Facebook constantly update our messages or newsfeed? I thought they fire an Alarm each second, but clearly that is impossible because the OS will push it up to a minute anyway. Using Handlers/Threads is off the question anyway because they don't fire when the app is closed, but WhatsApp updates your messages even when the app is closed.
So what exactly do these apps use if not AlarmManager to do the aforementioned task, and if they use AlarmManager, how do they set the short duration?
They have a service that uses push messaging. So basically they open a socket connection to a server and sleep until the server sends data to them. Its an interrupt mechanism, not a poll.
Not specific to any service like WhatsApp or Facebook, the idea behind this whole system works like this.
For Android in particular (or in general),
Your device registers with the Google Cloud Messaging (GCM) service
It allows GCM to create a HTTP persistent connection with your device
This connection is lightweight and persistent (stay alive for a long time)
With this, you will be able to consume these services, sending messages in this fashion.
Your Server ---> Google GCM Service ---> Target Device
When you send your messages, do not include the whole message itself. You should keep it lightweight by just sending simple messages to notify the targeted device that it has new information to be downloaded.
At your device application, you set up permissions and intent to listen for these push notifications. Whenever, there is new messages, you will make a request to your server to receive the corresponding messages.
Here's a guide to explain in depth: https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging
Cheers! Happy Programming!

The right approach to listen to GCM notifications

I'm trying to build a GCM notification listener, which will basically use the notification to flag the user that some operation should be made (which involve communicating with my remote App-Server).
I assumed that I should create a UI-less application running on the device's startup and listen to the GCM notifications and issue the internal android notification. When the user opens the notification an activity will be opened which will do the rest of the job with the remote App-Server.
Looking at notification examples it seems to me that I may be missing some basic understanding since all te examples which I had found use a UI application to manipulate the notifications.
What do I miss?
The common use case for handling of GCM messages in Android apps is as follows :
Your app registers to GCM upon startup and sends the registration ID to your server.
Your server sends a GCM message to your app.
You app receives the message in a broadcast receiver, which usually starts an intent service.
The intent service usually displays a notification to the user.
The user taps the notification, which starts an activity of the app.
You can see this use case implemented in the official GCM demo and in many other examples.
The fact that the app you wish to develop has no UI doesn't prevent you from implementing the exact same use case.

How can my Android app react to a push notification and download data in the background

I'm looking to make my app more responsive by having it automatically download some data (around 10KB) when it receives a notification. My two options (I think) are:
Try and pack all the info I need into the 4k payload limit - this may be possible, but certainly wouldn't help with iOS/WP implementations, as they have much lower limits. It also would be pretty inextensible, and I'd need to hand-craft the messages.
Send a notification which the app would react to and download data in the background.
If it's going to be 2, can I do that? Can it be done when the app isn't already running in the background or foreground?
For some updates I'd like to show a notification to the user. Can this be done in the same notification, or do I need to push another one?
Any similar info about iOS much appreciated, but not essential for answering the question!
When using Google Cloud Messaging you create a broadcast receiver which receives the GCM push notification. The broadcast receiver can either handle the notification on its own or start an intent service (which is better suited for your logic that downloads data from the server, since it runs on a separate thread, and doesn't block the main GUI thread). You can display a notification and download data from your server as a result of the same notification.
Look into BroadcastReceiver, once registered you can create a listener inside of your activity that will be called once the notification has been received.

ios alarm functionality limited?

I have an app which is on Android and iOS. I have added a local notification to fire every 24 hours at a time specified by the user of the app. In Android, the local notification functionality is exactly what I need, but in iOS it seems to lack the functionality I need, unless maybe I am missing something...
Lets say the user sets the time the notification is to fire to 11:00am. In Android, at 11AM, it will wake up the app, go to the broadcase receiver and I am able to run code in a method that calls out to an API to fetch the latest data. Once it gets the data, it posts the notification to the user.
In iOS, it seems the data being posted to the user has to be pre-scheduled. So I have to create the notification message during scheduling of the notification. What I need is to be able to do something more like the above example.
So the problem is that at the time of when the notification is scheduled to fire, I need to check for fresh data, not the day before...
Any suggestions?
The same functionality doesn't exactly exist on iOS.
You can setup a local notifications using the functionality of a UILocalNotification object. With this you can set fireDate, etc. which is sort of like a push notification without a server. You can send a message, add a badge on the app icon, play a sound, etc.
Now the issue is that the app doesn't get launched by the OS. The app simply registers a notification in the OS, which is then handled at the fireDate time. This means you won't be able to have a chance to check for data and verify whether to continue with the notification, etc.
UILocalNotification Class Reference

Categories

Resources