Notifications when the app is turned off - java

I wonder if it is possible to have notifications show up even if the app is turned off.
say for example I have an app and I set some kind of notification for 13.00 the next day and then turn the app off or restart my phone or something.
Is it possible to still get a notification in the notification bar with some text that starts the app or do the app need to run in the background to be able to use notifications?
if it is possible, what is the best way to do this? (get a notification at a certain time that starts the app) and if not, how do you recommend doing notifications?
The type of notifcations I want to do are reminder notifications.
thanks for all help!

A combination of AlarmManager and Service will help you to perform the task you are looking for, I had performed this thing.Check out the android tutorials for it.

What you're looking for is a background service. See this Android Services tutorial.
In the background service, your app should check periodically and when necessary, create and issue new notifications. See this Android Notifications tutorial.
The background service can be set to automatically start when the phone is restarted, and to re-issue any uncleared notification again.
Many apps such as Gmail already do this. Although you must be careful in resource usage or Android might kill your service.

Related

How can I make a background service for timer without showing a notification?

I have tried many solution but useless, all say use foreground service.
is there a way to trick android or to use a proper way.
i know it's restriction from android to inform the user that an app is draining the battery without using it.
The answer is use a foreground service. You can't have a background service for more than 2 minutes on modern Android.
Now if your timer is long (say 1 minute+) an alarm instead of a timer would work better. That wouldn't require a service, its a broadcast and your app would be restarted to handle it. Of course if you go into Doze those are still limited.

Ensure that notification show up on Android even if app is not opened

I am building an app that is needed for a research experiment.
The experiment is entirely based on users responding to notifications that are shown at somehow random intervals.
What is the best way to do this so that each notification will definitely be shown to the user?
WHAT IS DONE SO FAR
I have used AlarmManager and broadcast receiver but notifications stop showing up after some time. Notification Channels (with id) are used too.
I suspect the operating system removes the app after it has stayed in the background for a while.
Can anyone help to explain the best way to get the app to show notifications regardless?
Since this is an app for research, users are well aware of any slow performances, or battery usage. The only priority is to ensure that notifications show up. Without this, the whole experiment will fail.
Any help or pointers will be great! Thanks!
I'm not sure what method you've used to show notifications so I'm giving a general guidance.
You will need to use a background service which keeps running even after the app has closed. A background service can be killed by the android OS if its running low on memory, so you'll have to give permission to your app to autostart.
If you want even more reliability, you should use a Foreground service. This way you can give high priority to your notification service.
But it would be much simpler to integrate Firebase Cloud Messaging into your app than implementing your own NotificationBuilder service.

Java - android - change activity of app while running in the background

I'm developing an Android app with several types of alarms and triggers.
One of these alarms trigger if you stop moving (GPS tracking) while it's active.
Now, when the app runs in the background when this triggers the client doesn't update when you switch back in, and the only notification received is a push-notification from the backend service. If I enter through the notification, the client loads the alarm correctly.
The code base is quite extensive, and due to time and resources it would be best to avoid huge refactoring tasks.
Is there an easy way to make the app go from Activity A to Activity B when it's running in the background?
You cannot change the current Activity in the background without bringing the app to the foreground (using startActivity()). However, you can surely tell the app that when it is brought to the foreground it should start a certain Activity or rearrange the back stack or whatever.
Post some of the code and maybe we can help more.

Permanent services on android oreo

Android 8's battery consumption improvements are nice to the user but I am a bit afraid if my service will work as expected.
First of all: Thank you for any suggestions but I cannot just schedule my service. I want to make a OK Google-like keyword listener running in the background all the time. It will be based on the open source pocketsphinx-android library. I know that this will consume much battery power and I will inform the user about this.
Can we create a permanent background service on android 8+ ? I need to target android 8 in gradle because I was expecting some bugs with older targets. I also don't want to annoy a user with a foreground service which permanently shows a notification in the status bar.
[https://developer.android.com/about/versions/oreo/background.html] - Is there really no way of making permanent background services for my use-case (but preferably for all use-cases) possible?
Unfortunately, it's not possible to use a background service and don't show a foreground notification on Android 8.0 and higher.
The only one way that it might work is if you stick your app to Google APIs such as Voice Actions API.
As far as I know there is no a good work around and most apps like WhatsApp are still targetting Android API 24.
For what is worth, I am sharing me experience on that:
It's partially possible to use a background service and not showing a foreground notification on Android 8.0 just made some experiments and ended up with this:
Just create a notification channel with IMPORTANCE_NONE, and the notification will still exist, but will not be displayed in status bar.
Code excerpt:
NotificationChannel channelService = new NotificationChannel(
YOUR_SERVICE_CHANNEL_ID,
YOUR_CHANNEL_HUMAN_READABLE_NAME,
NotificationManager.IMPORTANCE_NONE);
channelService.setSound(null, null); // let's be quiet : no sound
channelService.setLockscreenVisibility(Notification.VISIBILITY_SECRET); // not on lock screen
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
// changes needs to uninstall app or clean app data, or rename channel id!
notificationManager.createNotificationChannel(channelService));
This way, the notification will not be displayed when your app is running in foreground.
Not a perfect solution:
When your app goes in background (i.e., you open another app), the "Android system" app displays a notification about "(your) app being running in the background".
So, this is not great, but, from my point of view, it's a bit better than before.

How to schedule my android app to do something every hour

I want my app to access database every hour and read next record from the table then update desctop widget and send notification. I know that there is AlarmManager which I can use to register my Intents but they are deleted when the phone is turned off or rebooted.
Is there any other android class/service that I would update my application continuously even when I reboot my phone?
Thanks,
take a look at the demo applications provided with android sdk
http://developer.android.com/samples/RepeatingAlarm/index.html
the look at AlarmService_Service for the implementation of the service once the alarm has been triggered
put all of the background tasks you want to do in your app in Services which perform tasks in the background. In the service you should be able to define a timer that causes whatever updates you want to occur every x hours.
In the onCreate() of the widget, start the service. onCreate() is called every time that the widget comes to life (such as when the phone starts if it is on the home screen) and will therefore guarantee that the Service is always running.
Hope this was helpful.
Just for the sake of completness, I am linking here the official docs, where the issue of the rebooting is adressed.
http://developer.android.com/training/scheduling/alarms.html#boot

Categories

Resources