send gps notification from everywhere - android

I have a list of coordinates (30+ pairs of longitudes-latitudes) and my task is the following:
I should create an android application which sends a notification (on the statusbar) to the user if he/she is on one of the 30 places that my list contains. The notification part is ready, i can do this in an activity (iterating through the list, if coordinates equal, then send the notification, but how can I do this for all activities, and for those times when the app is in cache, but not open? Thanks
Intent intent = new Intent(AGPS.this, SecondClass.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(AnotherGPS.this);
stackBuilder.addParentStack(SecondClass.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent
.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(AnotherGPS.this)
.setSmallIcon(R.drawable.cam_icon)
.setContentTitle("My notification")
.setContentText("content of notification")
.setContentIntent(pendingIntent);
NotificationManager NM = (NotificationManager) getSystemService(Context
.NOTIFICATION_SERVICE);
NM.notify(0, mBuilder.build());

If you are building below api26 you can use background service to listen for location changes, provided your application has permission and the location of mobile is on
Starting api 26 services will not work that way and you most likely get some exceptions, for doing background tasks you have to go through this https://developer.android.com/about/versions/oreo/android-8.0-changes.html#abll

you need to do this in a Service and not in the Activity
take look at this
https://stackoverflow.com/a/46025303/4705092
in onLocationChanged compare locations and if it fits you call notification part

Related

startForegroundService - unable to use setContentIntent + addAction?

Unfortunately another question about my startForegroundService notification... I searched, really, I did:
I have a foreground service that is running perfectly. I would like to add a couple of actions to this notification. For one, make it so when the user clicks the notification they are sent to MainActivity as well as adding a "Quit" addAction.
Here is the snippet I am using to create the notification:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My app")
.setContentText("Background service is running...")
.setContentIntent(pendingIntent)
.build();
startForeground(13365, notification);
Using the above a notification shows up just fine, but click on it results in nothing. I also tried using addAction, also nothing. I am aware the syntax is a little bit different (....Action.Builder) when adding an addAction.
I am creating my notification in the onCreate handler of the foreground service. Running on SDK 26.
Can startForeground notifications have setContentIntent / addAction attached to them?
Thanks!
Solved : I had the notification replaced elsewhere in my application and was not adding the intents there.
Doh!

How to receive notification when the application is closed?

I'm making an android application, and I want to receive notifications when the application is closed, how can I do that? I've read also other posts where people ask it but i can't understand how to make it work, could you please try to explain this better?
I've also tried to make a countdownTimer and make the application do something when the timer reaches the 0 but it didn't work, 'cause obviusly when I close the application, I close the timer too.
use this code to make in app notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

Do something when my app's notification is triggered

I'd like an activity of my app to listen when a specific notification is received and do something then.
The notification is triggered from within the app itself. I'm kind of new to Android development (and to development in general) so here's some pseudocode for what I want to do:
Activity{
notificationListener(){
if(notification is received){
//Do something
}
}
Any help would be much appreciated.
After checking the API of Notification, there seems no way to listen to sending Notification. After thinking deep, you would understand why we can't. AFAYK, it is we that write codes to send Notification but not the system, right? We surely know when we send Notifications, right? So, if you want to know when Notification is sent, why not do something, like sending BroadcastReceiver or starting Service to let other codes noticed? It all depends on our code-writers. And here is how to customize a Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

Send an Android Notification before an Activity has been launched

EDIT: The problem below disappeared with no changes in code. The notifications started appearing properly upon later reboots. This is at most an intermittent issue.
I have an application that starts up a background service when the phone boots. After a certain event is detected, I would like to send a notification to the user, allowing the user to elect to start my MainActivity if the user taps on the notification.
The problem is that Android seems to disallow sending notifications if no Activity has yet been launched. The code below is in a custom android.app.Application class, and works only if the Activity has already been launched.
Is there any way to send a Notification before an Activity has been launched?
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Event Detected")
.setContentText("Tap to launch MainActivity")
.setSmallIcon(R.drawable.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
Here is the error:
05-20 09:09:42.084: W/ContextImpl(801): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1505 com.android.server.StatusBarManagerService.sendNotification:981 com.android.server.StatusBarManagerService.addNotification:670 com.android.server.NotificationManagerService$7.run:2142 android.os.Handler.handleCallback:733

What is the difference between Notification and NotificationManger in Android?

What is the difference between those two?
I want to use the startForeground method and cannot use it with NotificationManager..
Thanks
A Notification is a class that represents either a persistent icon that goes in the status bar and is accessible through the launcher, turning on or flashing LEDs on the device, or alerting the user by flashing the backlight, playing a sound, or vibrating.
The Notification Manager is the class that allows you to add notifications to the system,
startForeground is a method of the Serivce class. For example inside your Service class you can have something like this.
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(someText);
Notification notification = builder.build();
startForeground(1, notification);
A Notification is a description of what you want to occur to alert the user about something -- what icon goes in the status bar, what ringtone to play, etc.
NotificationManager is a system service that can show a Notification.
I want to use the startForeground methode and cannot use it with NotificationManager
Correct. Create a Notification using Notification.Builder (or NotificationCompat.Builder). See this project for an example of using startForeground().

Categories

Resources