Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to call activity by clicking foreground service notification

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.

like image 603
user3137385 Avatar asked Nov 21 '25 10:11

user3137385


1 Answers

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);
like image 117
user2934536 Avatar answered Nov 24 '25 00:11

user2934536