So I am making an app that has to check for some information, but I need to be able to do that while the app is killed/not actively running. So basically like Youtube's notifications or something. I am a beginner and watched some tutorials on FCM, which can send a notification while the app is killed, which is fine. However, I need to be able to periodically make API calls, check if a certain condition is true and send the notification if so (all of that while app is not running). I tried googling that and found nothing that can help me. So... any ideas? (No code included since I don't think it's relevant.)
If this condition only affects the display of notifications, you can simply check the condition when you receive a notification through FCM. Here's an example :
class FCMService : FirebaseMessagingService()
{
override fun onMessageReceived(message: RemoteMessage)
{
val myCondition = getConditionFromAPI()
if (myCondition) {
showNotification(message)
}
}
}
Use AlarmManager to request periodic runs of your code. It may be set up to broadcast a message time to time to your receiver (declared in AndroidManifest.xml or in your code).
It will work while device is on. To continue after reboot, you have to set up a receiver for Intent.ACTION_BOOT_COMPLETED and register it in your code (not in AndroidManifest.xml, as it no longer works in latest versions of Android).
Related
I am using a NotificationListenerService to receive notifications. I use the method onNotificationPosted(StatusBarNotification sbn) to see when a new notification is, well, posted.
This all works great, however, I cannot get the sound uri from the notification.
Here is what I have:
int notiDefaults = sbn.getNoficiation().defaults //This is always 0
Uri sound = sbn.getNotification().sound; // This is always null
However, when I create my own test notification and explicitly do the following:
ncomp.setDefaults(Notification.DEFAULT_SOUND); // This makes the above code yield 1 for notiDefaults, does not change sound uri but is fine I can get the default uri easily
ncomp.setSound(myUri); // This makes the above code yield the value of myUri for "sound" when notificationPosted is called
The main problem I am having is this:
int notiDefaults = sbn.getNoficiation().defaults //This is always 0
Uri sound = sbn.getNotification().sound; // This is always null
occurs despite the notification actually playing sound (ie, Textra) plays sound by my NotificationListenerService says that it does not.
However, even in my test notification, if I do not explicitly set the sound or defaults, (meaning the notification channel says its type alert and will play a sound) the NotificationListenerService always yields null for sound uri.
What I am discovering is that no matter what I do in my onNotificationPosted method, if the notification does not explicitly set the defaults or sound uri, I cannot retrieve them. If one uses the notification channel options under android settings to set the sound, any NotificationListenerService will not get the notification sound uri to use for that notification.
If I had access to the NotificationChannel that belongs to this Notification then I can determine everything needed. But, there is no method which allows third parties to get a NotificationChannel of another app.
This seems to be broken google code or I am missing something.
EDIT: Testing on Android 11 Samsung Galaxy S20+
Thanks all!
As soon as I receive a push notification from my app I want to trigger the KeyguardManager to launch the fingerprint/pass code screen to open the phone from lock screen so that person can enter the phone and unlock the device.
I want to trigger this programmatically similar to when we click on any notification from lock screen we get the fingerprint/pass-code screen.
I did a lot of RnD but didn't find any solution, this is one of the challenging use case task given to me in class, I have been exploring a lot from quite few weeks with no success at all.
Did tried Broadcast receiver with BiometricManager and many things with no success, any lead will be very helpful.
As soon as you receive push message, onNotificationReceived() (or some other method if you use some 3rd party libs) method gets called as below. from there, you can launch your Main screen where you have written biometric/unlocking code.
class MyReceiver : PushReceiver {
override fun onNotificationReceived(message: Message) : Boolean {
//Launch your MainActivity where you can show Unlock screen.
return super.onNotificationReceived(message);
}
}
I'm trying to program a remote alarm clock application for Android, because my friend sometimes misses her train or is too early (I don't want to wake up, but just want her to set my alarm clock time).
I figured out how to set the alarm clock using Java in just a couple of minutes, but that function must be executed remotely. How to do that?
At the moment I'm using Googles Firebase Cloud Messaging service to send notifications including the time for the alarm clock as parameters.
However, when I receive them and the app isn't open, it doesn't execute any code, but waits until the user taps on the notification. But what I want the app to do is set the alarm clock automatically when a notification is received.
I know this does probably violate a bunch of security guidelines, but this isn't an app for the PlayStore, but just for myself.
My idea is to built some kind of service that's active all the time in the background waiting for notifications. Do you think something like that is possible?
Thanks for your help :-)
Try to pass notification payload to the service class and there you can set alarm manager in the background.
We cannot get notification information when app killed or at background, but if notification arrived, the onCreate of FirebaseMessagingService will be fired, so we could try to get notification information from your backend server in onCreate.
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "in onCreate, try to get informations from backend server, ex..send http request here");
}
I want to make a notification which cannot be cancelled by the user manually, just like google navigation notification. I don't know how to do it.
Please Help.
Below is my code that I had tried.
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentIntent(pendIntent)
.setContent(mRemoteViews)
.setPriority(0)
.setTicker(ticker);
Thanks.
Try adding this
mBuilder.setOngoing(true)
Other option is to use intentService or background service to make your notification for you instead of the application context.
I know that the SMS content provider is not part of the public API (at least not documented), but if I understand correctly it's still possible to use many of the SMS features as long as you know how to use the API(?).
E.g it's pretty straightforward to insert an SMS into your inbox:
ContentValues values = new ContentValues();
values.put("address", "+457014921911");
contentResolver.insert(Uri.parse("content://sms"), values);
Unfortunately this does not trigger the standard "new-SMS-in-your-inbox" notification. Is it possible to trigger this manually?
Edit: AFAIK the "standard mail application (Messaging)" in Android is listening for incoming SMSes using the android.permission.RECEIVE_SMS permission. And then, when a new SMS has arrived, a status bar notification is inserted with a "special" notification id.
So one solution to my problem (stated above) could be to find, and send the correct broadcast intent; something like "NEW SMS HAS ARRIVED"-intent.
Edit: Downloaded a third party messaging application (chompsms) from Android market. This application satisfies my needs better. When i execute the code above the chompsms notice the new sms and shows the "standard status bar notification". So I would say that the standard Android Messaging application is not detecting sms properly? Or am I wrong?
Unfortunately the code responsible for these notifications is hidden in the messaging application. The class MessagingNotification has a static method updateAllNotifications that you could call using a PathClassLoader and reflection:
PathClassLoader c = new PathClassLoader("/system/app/Mms.apk", getClassLoader());
Class.forName("com.android.mms.util.ContactInfoCache", true, c)
.getMethod("init", Context.class).invoke(null, context);
Class.forName("com.android.mms.transaction.MessagingNotification", true, c)
.getMethod("updateAllNotifications", Context.class).invoke(null, context);
This is obviously a very bad idea for several reasons but I can't think of another way to do what you described.
Could you trigger a PUSH notification after the SMS?
Thread: Does Android support near real time push notification?
Maybe you should replace
content://sms
with
content://sms/inbox