1

Я показываю Notification для фона Service с использованием метода startForefround().Пользователь Android Отключает мое уведомление и возвращается в мое приложение

if (notif == null) { 

     // Create the pending intent 
     Intent intentForeground = new Intent(this, BackgroundLocationService.class) 
     .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
     PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0); 

     notif = new NotificationCompat.Builder(getApplicationContext()) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setDefaults(Notification.DEFAULT_ALL) 
     .setTicker(getText(R.string.location_service_starting)) 
     .setOnlyAlertOnce(true) 
     .setOngoing(true) 
     .setContentIntent(pendIntent) 
     .build(); 
     notif.flags |= Notification.FLAG_NO_CLEAR; 
    } 
    startForeground(notificationID, notif); 

Он правильно показывает уведомление пользователю, и когда я скольжу вниз панель уведомлений я хотел бы, чтобы пользователь нажмите на уведомление и вернуться в мое приложение. Как я могу это сделать?

ответ

1

Используйте этот

Notification notification = new Notification(R.drawable.logo2, "app is running on the background!", System.currentTimeMillis()); 

notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

notification.setLatestEventInfo(this, "title", "message!", contentIntent); 

this.startForeground(1023, notification); 
+0

Ах, я пропускал часть MainActivity.class. Благодаря! –

Смежные вопросы