Do something when my app's notification is triggered - android

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());

Related

Handling FCM Notification while running and killed

I am searching for a pattern for handling fcm notifications while the app is running, in background or killed.
Our app features a login process and therefor when the app is not launched, the notification needs to open the launcher, but when the app is running i want to just push an activity when the user clicks the notification.
So the best way i guess would be an ContentIntent that gets either delivered to the top most Activity in the Stack or delivered to the Launch-Activity.
My current code always relaunches the app with a TaskStackBuilder no matter if the app is running or not.
A sample of my code:
resultIntent = new Intent(this, MyProfileActivity.class);
resultIntent.putExtra(ActivityUtil.KEY_IMAGE_URL, SharedPref.getUserImage(this));
resultIntent.putExtra(KEY_NOTIFICATION_ID, notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SetDetailReworked.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "...")
.setLargeIcon(getBitmapFromUrl(images[UI.GetDensityInt(this)]))
.setSmallIcon(R.drawable.bar_icon)
.setContentTitle(getString(R.string.push_notification_Title))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(resultPendingIntent);
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID, not);
Try to set your MyProfileActivity's launchMode as singleInstance in androidManifest,
<activity
android:name=".MyProfileActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
and it will not relaunches your background activity when user clicks the notification. Instead, MyProfileActivity's onNewIntent will be fired.

send gps notification from everywhere

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

Android Notification Click - don't backstack activity

I have a service in background which gives a notification.
I want the next thing:
If I click the notification and the app is not opened open the XActivity.
If the app is opened and the XActivity is created, go there and don't recreate the activity (because if this happen, on back key i will see the same activity again).
My notification code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New posts!")
.setContentText("New funny posts had just arrived! Click here to see them!");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, XActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I start the service from the XActivity. (just a activity name example)
Thank you.
i had the same problem, what i did was , i added this line android:launchMode="singleTop" to my activity in manifest, and it worked.
Problem for duplicate activity in your case is because of standard structure design of Activity in Android. For creating only single instance of activity (no duplication) you need to give launchMode property to your activity in your manifest file. You can use singleTop property for this behaviour. You can read more in provided link.
In Addition, for new data access you can use onNewIntent() for new intent coming to this activity and set intent using setIntent() and continue working with new data.
Hope this helps.
Thanks.

PendingIntent opens main activity instead of desired activity

I am creating an android application that shows a notification in a Service.
I show the notification exactly as mentioned in documentation. It works fine and the PendingItent object starts the desired activity.
But after a while when I close and restart the application a couple of times, tapping on the notification will start the Main Activity (which is the parent of desired activity) instead of the desired activity.
The only way I found to fix that is to restart the device. (Clearing app's cache and uninstalling didn't help.
I googled my problem and also searched here a lot, but couldn't find anything relevant.
seems that I'm the only one having this problem.
Could anyone please help me with this problem?
Thanks in advance;
EDIT:
as I said, I do exactly like the documentation. But here is my code anyway:
Intent resultIntent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MyActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Body");
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(457, builder.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

Categories

Resources