Ok,
I am working on an Android app that uses a service to collect & process GPS data. The service also creates a notification in the status bar. What I cannot figure out is how to have the notification open the main class when a user clicks on it. From what I've found online it shouldn't be that hard to do, but I cannot get it to work. Does anyone have some suggestions on where to look?
Intent notificationIntent = new Intent(this, MyClass.class);
This line of code is from http://developer.android.com/guide/topics/ui/notifiers/notifications.html.
Simply replace MyClass with your activity class - it will be launched when you click the notification.
Related
I need to implement this feature but I am not sure if this is possible on android.
We have gcm listener service running. But let's say the app is open, and a notification arrives from the server. I need to have an activity triggered automatically without touching the notification status bar on the top of the phone screen.
In other words, without any user interaction once the notification arrives, if the app is running, an activity must be triggered immediately.
I took a look at this thread, but this is not really what I need.
Intent - if activity is running, bring it to front, else start a new one (from notification)
any clues or more info?
thx!
You can start an activity without another prior activity by using the FLAG_ACTIVITY_NEW_TASK flag.
Context c = getApplicationContext(); // or getContext(), or any other context you can find from the current app
Intent i = new Intent(c, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
i'm a beginner in android , so excuse me for a perhaps stupid question. I'm developing an App with different Features, wich are chosen in the Menu. By google cloud Messaging the App is also receiving push notifications, which are Stored in an MySQL database. These notifications can be shown in a second listactivity. Now my Problem: when this activity is open and a notification is coming in, it is Stored in database, but the aktive listactivity is not updating, Cause it doesn't know. Howe can i Force my listactivity to Refresh from mainactivity when mainactivity is receiving a notificatipn ? Thanks from Germany Fritz
You can post o a notification (for example from a Service) and pass an intent to the corresponding pendingintent.
Intent intent = new Intent(this, MyListActivity.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent pi = PendingIntent.getActivity(this, my_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
notificationManager.notify(my_notification);
And in your MyListAcitity you can override the method onNewIntent and reload your data.
I am fairly new to Android, and am currently working on a simple XMPP Client. A user should be able to log in, and should be notified whenever an XMPP message arrives. It should be possible to access an overview of all messages that arrived during the current session.
When launching the application, a LoginActivity is started, prompting the user to fill in his or her credentials. If the right credentials are provided, a background service is started:
Intent intent = new Intent(this, NotificationService.class);
startService(intent);
On startup, the notification service adds a packet listener to the XMPP connection and requests to be running in the foreground. The users is prompted with a notification caused by this foreground request ("Ongoing"). Now I have provided a second activity called XMPPClientActivity, showing all messages that are received during the session and a simple logout button. When opening the application from within the "Ongoing" notification, the XMPPClientActivity is started because the notification is defined like this:
xmppIntent = new Intent(this, XMPPClientActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, xmppIntent, 0);
NotificationCompat.Builder xmppBuilder = new NotificationCompat.Builder(this);
xmppBuilder.setContentIntent(pendingIntent);
// Notification details
startForeground(id, xmppBuilder.build());
When opening the application from the home screen however, the LoginActivity is opened again. Of course I want the XMPPActivity to be started, but I can't seem to figure out how this should be done. I have been looking into binding an activity to a service, but I'm unsure if this can be of any help. What is the right way to do this?
What you can do, as I understand you issue, is use ShaeredPreferences. Create a preference like "loggedin" and set a boolean variable to true the first time they log in. Now you can set this to false when they click the "logout" Button.
When the Activity is started you can check the SharedPreference before calling setContentView() and if the value is true then finish() the LoginActivity and open your other Activity.
The link to the docs I provided has a good example of creating, opening, and editing SharedPreferences
I've been searching for this for a while and keep coming up short. However I have setup notifications before where selecting it launches an activity, now I'm trying to mod that code.
This time I'm looking for a solution to run just a single line of code when the notification is selected. I want it to set a button from invisible to visible. So when my notification is cleared I want the code below to take place, not launch an activity or intent:
butNX.setVisibility(0);
Any input on the matter? thanks in advance.
I wasn't try this, but i think it will resolve your problem.
1. Set up a Receiver where you need it.
2. Create an Intent and add the extras to it something like this: intent.putExtra("MODE","VIS");.
3. Craete a PendingIntent which will broadcast your intent to your receiver.
PendingIntent pIntent=PendingIntent.getBroadcast(yourContext,yourReqCode,
yourIntent,yourFlags);
4.Set notification
So while receiver will receive your broadcast it should to check what to do with this intent by data which you send...
In this code snippet, what Activity class do I use? The one where the code is written? Is this statusbar message going somewhere?
Intent notificationIntent = new Intent(this, MyActivity.class);
Does this method act immediately or it's done later?
NotificationManager.notify();
In this code snippet, what Activity class do I use?
How should we know? We are not mind readers, and you have given us one line out of a much larger example somewhere.
Taking a completely random guess, that is probably the activity that should be opened when the user taps on the notification entry.
Does this method act immediately or it's done later?
The call is asynchronous, but it should be displayed almost immediately.