I have an activity in my app. When i click one option, start running my service foreground with a notification like this:
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), Radio.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification();
notification.tickerText = "Connecting RadiO!.. Wait..";
notification.icon = R.drawable.ic_launcher;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "radiO!",
"Playing: " + songName, pi);
startForeground(1, notification);
Then, still running my activity, I click on my notification and I opened it without closing the previous activity. I must not have the same activity twice.
What you are doing is creating a new instance of the activity you are trying to execute each time you click on the notification, in order to avoid it and reuse an already existing activity(if any), just set the activity as singleTask in the AndroidManifest.xml as shown below:
<activity
android:name=".YourActivity"
android:launchMode="singleTask"/>
Hope this helps.
Regards!
Related
I have a Service running in the background and at certain periods of time I create notifications with Notification and when the user clicks the notification, it opens an Activity (SurveyForm). I want to close that Activity when the user presses a button but leave the background Service running. I am calling the finish() method within the Activity but instead of closing it completely, Application is still present in the recent app list.
This is the code that creates the notification.
Intent notificationIntent = new Intent(this, SurveyForm.class);
notificationIntent.setAction(Constants.SURVEY_ACTION);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Survey")
.setTicker("New Survey")
.setContentText("Please, answer the survey.")
.setSmallIcon(R.drawable.survey_notification_icon)
.setSound(alarmSound)
.setLights(0xff00ff00, 1000, 1000)
.setContentIntent(resultPendingIntent)
.setOngoing(true)
.build();
notification.defaults = Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(Constants.SURVEY_NOTIFICATION_ID, notification);
And the button code is the following:
bSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Close the notification
nm.cancel(Constants.SURVEY_NOTIFICATION_ID);
//Close this Activity
finish();
}
});
Update
I have solved the problem by specifying the next property in the manifest file for the corresponding Activity.
android:excludeFromRecents="true"
I have solved the problem by specifying the excludeFromRecents property in the manifest file for the corresponding Activity.
android:excludeFromRecents="true"
Call finishAndRemoveTask() instead of finish()
I start a foreground service which shows a notification. If my activity is hidden I want it start by clicking on the notification.
A function called in onStartCommand does this:
startForeground(noti_id, mNoti);
The notification appears and works but it doesn't reactivate my MainActivity:
notiIntent = new Intent(this, MainGate.class);
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
MainGate.class is the activity which starts the foreground service. It should appear when I click on the notification.
EDIT:
Actually, it worked when the notification was built in the man activity (MainGate.class). And it worked when notification was built in the service not being foreground service. Now I had to implement the foreground service and it stopped working.
try this solution
Intent notificationIntent = new Intent(this, MainGate.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(getString(R.string.isRecording))
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION_ID, notification);
I am creating a notification from a service; the notification is shown, but when I click on it, nothing happens: It was supposed to open an activity.
My code:
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
However if I use pretty much the same code from inside an activity, I can click on the notification, and my activity is shown. What am I doing wrong?
EDIT:
It turns out that there was nothing wrong with this code, there was a different issue:
When my service finished, it created the notification with the code above. However, the service also broadcasted that it was finished, and the receiver created another notification, which used a different code to create the notification (with no PendingIntents, so no defined action when the notification is clicked), and that notification must have placed itself instead of the original, correct one.
On top of using Notification.Builder for above Android 3.0, or NotificationCompat.Builder in support library v4 as #Raghunandan suggests in the comment, I had the same problem with a possible common solution to your problem.
This is specific to 4.4 as seen here:Issue 63236:Notification with TaskStackBuilder.getPendingIntent() is not open the Activity and here Issue 61850: KitKat notification action Pending Intent fails after application re-install
One confirmed solution is to perform cancel() operation on an identical PendingIntent with the one you are about to create.
What worked for me was to modify the target Activity's manifest definition and add
android:exported="true" within "activity" tags for the target Activity. That would be MainActivity in your case I assume.
Example:
<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>
This works with api level 8.
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
you can create notification like this :
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = mContext.getString(R.string.noti_comes); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t); //Full title of the notification in the pull down
CharSequence contentText = clck_see_noti; //Text of the notification in the pull down
//you can set your click event to go to activity
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, MainActivity.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
and do not forget the your service is registering in the manifest
<service
android:name="com.xx.your_service"
android:enabled="true"
>
</service>
If you define an Activity that is not registered into the AndroidManifest.xml the notification will not show any error message, and nothin happen.
Intent notificationIntent = new Intent(this, MainActivity.class);
be sure to have the Activity to start from the notification registered into the AndroidManifest.xml
This guy had a similar problem "I misspelled my activity name in the manifest.":
launch activity from service when notification is clicked
Im my case my project use Android Annotations. So my error was when I created the Intent, I always set this:
Intent notificationIntent = new Intent(this, MainActivity.class);
But is necessary to set the Android Annotation's generated class:
Intent notificationIntent = new Intent(this, MainActivity_.class);
That's solves my problem
I use the following code to add an icon to Notification status, a user can click the icon to open the app ui.SMSMain.class, and the two ui.SMSMain.class apps will be opened if the user click the icon two times.
I hope the app only can be opened one time, how can I do?
private static void ShowNotification() {
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.smsforward,
myContext.getString(R.string.app_name),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
CharSequence contentTitle =myContext.getString(R.string.NotificationTitle);
CharSequence contentText = myContext.getString(R.string.NotificationContent);
Intent notificationIntent = new Intent(myContext, ui.SMSMain.class);
PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,
notificationIntent, 0);
notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent);
notificationManager.notify(NotificationID, notification);
}
add launchMode attribute to your activity in the manifest as SingleTop
<activity
android:name="Tracking"
android:label="#string/app_name"
android:launchMode="singleTop">
....
this will allow only one instance of the activity, also it is better to use flag PendingIntent.FLAG_UPDATE_CURRENT in your pendingIntent
PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
though, I am not sure if 0 actual refer to UPDATE_CURRENT
Update
you may need to choose among singleTop or singleTask according to your needs
for singleTop: if an instance of activity already exists at the top of the current task and system routes intent to this activity, no new instance will be created
for singleTask: A new task will always be created and a new instance will be pushed to the task as the root one. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call.
for more information about LaunchMode.
I believe that singleTop suits your problem better.
I have MyActivity and MyService (which plays music). I have also notification (in Notifications bar) but when I press it, MyActivity opens up in default state (play button is not pressed for e.g., although the music is still playing). This worked OK before I started to work with binding - I am concerned it has to do something with it.
What could be wrong?
EDIT:
This is my Noification inside my service:
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), SoundRelaxerActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification();
notification.tickerText = "Soundrelaxer: "+trackTitle;
notification.icon = R.drawable.play;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "SoundRelaxer","Playing: " + trackTitle, pi);
startForeground(1, notification);
Try adding this flag to the Intent you're using in your Notification:
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
That way a new instance of your Activity will not be created if it exists.