Custom Notification - android

I would like to know if there is a way of using the notication bar in order to do some operations (onClick), without having an activity being launched/resumed.
for example.. let's say i raise a notifcation, and when the user press on it, then instead of take me to some activity, it invoke some regular method in my current activity/service
Is there any way to implement such a thing?
for example the current notifcation code do a standart behave onClick. running up an activity.. how will channge the code in order to invoke some method instead of an activity?
messagesManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, message,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, someActivity.class), 0);
notification.setLatestEventInfo(context, "notification", message,
contentIntent);
messagesManager.notify(R.string.noto, notification);

Is there any way to implement such a
thing?
Use an appropriate PendingIntent. Instead of calling getActivity(), call getService() or getBroadcast().

I don't believe this is possible or, at the very least, best practice. It's possible that Android could kill your activity while the Notification is still waiting in the top bar. For example maybe you get a phone call but Android is low on RAM - it kills your activity, thus there's really not a 'current' activity anymore.

Related

Stacking notifications from GCM is not working with setGroup

I have the following notification logic inside a class that extends GcmListenerService, and gets called when one notification arrives. Then, when clicked, the app takes you to MainActivity where the notification is displayed properly.
public static void mostrarAvisoBarraEstado(Context context, String alerts)
{
Intent notificationIntent = new Intent(context.getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("alerts", alerts);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(),
notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText("Alert received")
.setSmallIcon(R.drawable.nubeazul)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setGroup(GRUPO_ALERTAS)
.setGroupSummary(true)
.setAutoCancel(true)
.build();
//notificationManager.notify(0, notification);
notificationManager.notify (new Random().nextInt(), notification);
}
So, right now, each one is displayed separately, and if they build up, the result is rather ugly with all the notification bar full of little icons. Can you guys help for an elegant solution since I am kindda new to Android? Thanks a lot!
NEW STUFF ADDED today!
If I take the notify random out, leaving something like notificationManager.notify(0, notification);, I will get just one notification, but nothing else, then when it launches MainActivity (its onResume() method) it will only display one notification and all the "piled up ones" are just discarded when clicked on the one notification. What I want to achieve is that while maintaining a clean display, i.e: one group notification for all GCM, if I click on the group, I will get each and every notification displayed throught the Alerts.class (something like looping through the notifications, and starting the activity Alerts for each one.
#Override
protected void onResume()
{
super.onResume();
if (getIntent().hasExtra("alerts"))
{
Bundle extras = getIntent().getExtras();
Intent intent = new Intent(this, Alerts.class);
intent.putExtra("alerts" , extras.getString("alerts"));
startActivity(intent);
getIntent().removeExtra("alerts");
}
}
Then the Alerts class will nicely display the alert which it does, but one per notification.
So I tried out your code and managed to see reproduce what you were encountering (I just manually created dummy notifications). So the reason that the Notifications were piling up was because the id you are passing in the notificationManager.notify() is different from one another. As to what I have observed on the Notification and NotificationManagers behavior so far, the id indicated in notify() it kinda represents the id location/position (not sure what to call it) of the Notification that is under the NotificationManager, not an id of the Notification itself.
Imagine the NotificationManager as an array or list. If for example, you have 3 visible notifications on the Status Bar:
Notification 1, Notification 2, Notification 3 and their ids are as follows: 0, 1, 2.
If you generate a new Notification 4 then called notify passing it as a parameter, and the id 1, the notifications that will be currently shown in the NotificationManager would result to something like this:
Notification 1, Notification 4, Notification 3 and their ids are as follows: 0, 1, 2.
So the reason that you're notifications are piling up and NOT grouping together is because you end up with different ids when calling notify() while passing new Random().nextInt().
As per the behavior that I think you are aiming for -- Joining the notifications from your app into one -- the implementation is simple when understood, but I think it's still a little bit tricky. It's like you have to check first if there is more than 1 notification already, then if yes, you create a summary notification with the details and you show it alone (mind the id ;)) and all of those other stuff. I found this blog though that I think might help you with it. Or you can simply just check out the official docs on Stacking Notifications.
So bottom line, simply use a single id to pass in notify() when it comes to your apps Notifications. Hope this helps. Cheers! :D

Android service is frequently stopped and restarted

I'm trying to develop an Android application which draws a floating overlay on the screen, as it is done by Facebook messenger with chat heads.
I've created an Android service from which I handle the UI. Everything works well, but on some devices the service is stopped very frequently and sometimes it is started again after more than 60 seconds.
I know this is a behavior defined by the Android System, but I was wondering if there is a way to give my service the maximum priority. Is this possible? Could this behavior be worsened by something in my implementation which is wrong?
One option is to make your Service a "Foreground Service" as briefly explained in Android documentation. This means that it shows an icon and possibly some status data in the status bar. Quoting:
A foreground service is a service that's considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory. A foreground service must provide a
notification for the status bar, which is placed under the "Ongoing"
heading, which means that the notification cannot be dismissed unless
the service is either stopped or removed from the foreground.
In practice you just need to modify the Service's onStartCommand() method to set up the notification and to call startForeGround(). This example is from the Android documentation:
// Set the icon and the initial text to be shown.
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis());
// The pending intent is triggered when the notification is tapped.
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// 2nd parameter is the title, 3rd one is a status message.
notification.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), pendingIntent);
// You can put anything non-zero in place of ONGOING_NOTIFICATION_ID.
startForeground(ONGOING_NOTIFICATION_ID, notification);
That's actually a deprecated way of setting up a notification but the idea is the same anyway even if you use Notification.Builder.

How do I make a Statusbar notification that does nothing and goes nowhere?

I have a working statusbar notification for Android already, but to create it I must set an Activity for it to open. I don't want any activity to open; however, this small project doesn't need an interface at all.
mNotificationManager = (NotificationManager) aContext
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
// Create the pending intent, which is basically NOW.
PendingIntent contentIntent = PendingIntent
.getActivity(aContext, 1, new Intent(aContext, BlankActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
// Create notification
notificationBuilder = new NotificationCompat.Builder(aContext)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher) // required for launch
.setTicker("Downloading video...") // required for launch
.setWhen(System.currentTimeMillis()) // should be set for now.
.setContent(remoteView);
BlankActivity is what it sounds like -- an activity that has no content and closes upon being opened. But it still shows up on the list of recently opened windows. I cannot set this to null.
Is there a way I can avoid setting an intent for the status notification at all?
You don't need PendingIntent in that case. Just setting it null in setContentIntent() might help.
Another way can be
PendingIntent contentIntent = PendingIntent.getActivity(aContext(),0,new Intent(),PendingIntent.FLAG_CANCEL_CURRENT);
Try this.
Setting it to null does not work for early versions like SDK 2.3 which requires an intent to be set, otherwise android will choke at receiver's start with the exception:java.lang.RuntimeException: Unable to start receiver com.movistar.android.mimovistar.widgets.WidgetIntentReceiver: java.lang.IllegalArgumentException: contentIntent required
see this post

intent extras are duplicated when using FLAG_UPDATE_CURRENT in PendingIntent when creating android notifications

I want to create several notifications that launches an activity (or refresh it) to display a product description.
Notification notification = new Notification(R.drawable.applicationicon,
Resources.getString("NewSaleNotification", context),
System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, MainApplication.class);
intent.putExtra("saleid", saleid);
// to be sure the activity won't be restarted
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, SaleTitle, SaleMessage, pendingIntent);
notificationManager.notify(saleid, notification);
When I create the PendingIntent, I have 4 choices : FLAG_CANCEL_CURRENT, FLAG_NO_CREATE, FLAG_ONE_SHOT and FLAG_UPDATE_CURRENT.
The definition of the last one (http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT) is what I want to do but it doesn't work as it should. If I create 2 notifications, they both have the same 'saleid' extra which is the latest one. How can I make more than one notification with different 'saleid' extra?
but it doesn't work as it should
Yes, it does.
If I create 2 notifications, they both have the same 'saleid' extra which is the latest one.
This is precisely what the documentation says is supposed to happen.
How can I make more than one notification with differents 'saleid' extra?
Option #1: Put a different action string in each of your Intents. This will make them different (from the standpoint of filterEquals()) and give them separate PendingIntents. However, since you are specifying the component in the Intent (MainApplication.class), the action will not affect how the Intent is routed.
Option #2: Use a different requestCode (2nd parameter) on your getActivity() calls. While this is documented as "currently not used", it does result in different PendingIntent objects being returned. However, since this behavior is undocumented, it may change in the future.

Correct way of using android notifications to pass data to my application

Hallo!
I have a question concerning notifications.
I want to create an application, that parses received SMS for a certain content. If the sms contains a certain content it is parsed and the data is extracted. A notification should be created and it should show up. When the user selects the notification, the application should be invoked with the extracted data. If the application is not running, it should be started. If it is already running, the instance should be displayed and processing the information.
To solve this I did the following:
A Broadcast receiver listens for SMS and parses them. To invoke the application I use the following code:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, tickerText, when); Bundle bundleNotification = creatingNotificationBundle(...);
Intent notificationIntent = new Intent(Intent.ACTION_MAIN); notificationIntent.setClassName(...); notificationIntent.putExtras(bundleNotification);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify((int) System.currentTimeMillis(), notification);
The Activity that I invoke processes the Intent in the onCreate(...) method. This works fine when the application is not running. When I already have an instance running, it is invoked and no data is passed (or I'm not overwriting the correct methods). I have seen the an onResume() is invoked. But this method doesn't have an intent as a parameter. What is the best idea to solve this problem?
I did an (in my opinion) very dirty workaround to get this work:
I created a temporary activity
I do the same code as above except that I invoke this new activity and set the flags notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) and notificationIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
this activity sends first a Broadcast and than an Intent both forwarding the data previously parsed.
Using this way the main application alwas gets the data. If it is already running it receives the broadcast and comes to front with the intent, if it is not running, it receives the data from the intent in the onCreate(...) method.
But I don't think that this is the correct solution for this problem, since it seams quite dirty to create a dummy activity just for redirecting data and doing nothing else.
Any ideas and suggestions would be highly appreciated.
Thanks
You probably want to override Activity#onNewIntent(android.content.Intent):
…when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

Categories

Resources