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.
Related
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've a notification which says "Now Playing : Radio " and it has a stop button below this. When the user clicks on stop button, the audio stops and now what I'm trying to achieve is when the user clicks on the notification part above the stop button I want to open a fragment. I'm showing the notification like this.
Intent notIntent = new Intent();
notIntent.setAction(ACTION_STOP);
PendingIntent pendInt = PendingIntent.getService(this, (int) System.currentTimeMillis(),
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.music_nepal_not)
.setColor(ContextCompat.getColor(BackgroundAudioService.this, R.color.colorPrimary))
.setTicker("Playing Live Radio")
.setContentTitle("Now Playing : Live Radio ")
.addAction(R.layout.toggle_notification, "", pendInt)
.build();
notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, n);
You can do something like:
n.addContentIntent(pendingIntent);
This would get triggered when the user clicks on the notification body. pendingIntent would be a PendingIntent to your Activity hosting the fragment since, those bastards cannot exist without activities. Similarly, you can add that pendingIntent to your addAction().
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!
I would like to create a notification that clears automatically when the user presses it and has no other behaviuour. The notification below works fine but when i press on it it takes me to the activity "MyActivity" (even having to define an intent seems a bit unecessary when I don't want to use it...)
Using FLAG_AUTO_CANCEL doesn't seem to have any effect at all.
Update: Sorry, I have found the FLAG_AUTO CANCEL does work, that is the notification is cleared from the status bar. I guess I am really tring to write an intent that does nothing (or completely delete the intent).
Code...
Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());
//Define the expanded message and intent
Context context = getApplicationContext();
CharSequence contentTitle = res.getString("My content title");
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//Pass the notification to the notification manager
mNotificationManager.notify(1, notification);
Use an empty intent without an actual action.
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
I don't think you can. You can have MyActivity exit immediately though: call finish() from onCreate().
I am a bit lost with PendingIntent.
As far as I could understand, it's a Token given to the OS to perform later (hence pending) operations.
I have an activity that launched a service. The service, occasionally creates a notification.
What I am trying to do, as the simplest of all, is to bring the activity to the front.
I am not sure where and how I create and to whom I send the PendingActivity.
If I create it within the Activity, I need to send it to the service - HOW?
If I create it within the service, how would the context be to call the activity? are these the same? - I though these are the same, as how the OS works, but it did not work for me.
Here are some code lines
This is not working btw StartService gets an Intent.
This code is in my activity
Intent intent = new Intent(this, NeglectedService.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_ONE_SHOT);
startService(contentIntent);
So, the correct one is
Intent intent = new Intent(this, NeglectedService.class);
startService(contentIntent);
So I think to make the pending intent in my service, but this didn't work for me, as I am not sure how to reuse/use the intent
Notification notification = new Notification(R.drawable.icon,
extra,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent, // not sure what intent to use here !!!!
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;
mNotificationManager.notify(id, notification);
solved
What needed to be done, is use the Neglected.class in the intent.