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);
Related
Steps:
We are using android WorkManager
Ref link:: https://developer.android.com/topic/libraries/architecture/workmanager/basics
Create a notification for foreground service
App goes in background, we start the foreground service using:
startForegroundService()
Ref:: https://developer.android.com/guide/components/foreground-services
Issues::
App goes in "Idle mode" foreground service is not working.
Correct behaviour we need:
App goes in background
App killed from background
Phone goes in "Idle mode"
In above 3 case foreground service is running continue without stop.We need this functionality in all android devices.
Current Android device version and modal:
Android: 11
Modal: CPH1969
Note:: In android documentation mentioned, If start the foreground service it will be never stop If app goes in "Idle Mode".
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
Intent fullScreenIntent = new Intent(this, LockscreenActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setTicker("Background service ticker")
.setFullScreenIntent(fullScreenPendingIntent, true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.build();
startForeground(1, notification);
startService(intent);
return START_STICKY;
Requirement:
I need a foreground service for track the location like one location to another location.
I have a service with a foreground notification.
How can launch an activity when user tap on the foreground Notification?
I have tried with:
// CREATE NOTIFICATION FOREGROUND
Intent notificationIntent = new Intent(getApplicationContext(), HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Title")
.setTicker("Test service")
.setContentIntent(contentIntent)
.setContentText("text...").build();
startForeground(1, notification);
//======================================================================================
i need to start HomeActivity when user tap foreground notification.
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 have a setup where my activity starts a service and sends values through the intent that control how the service acts. These variables can change through the life of the service, and the service pushes updates back to the activity UI. If I exit the activity and relaunch it, it will request an update from the service to get the current values. This works fine if I re-launch the activity via the home screen, or if I use the recent apps menu. However if I launch the activity via the run in foreground ongoing notification I have set for the service, then the service re-sets itself to the original values when it was started.
Here's my run in foreground code, I don't think there is anything else that would have an affect on this problem.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);
Update 3:
Adding the flags Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP, and Intent.FLAG_ACTIVITY_SINGLE_TOP to the notification intent fixed the problem. I have no idea why these fixed it. If anyone can tell me why this happens please do. Here's the new code that works.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);
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.