Notificación de Android en segundo plano si la aplicación se cerró?
Estoy intentando mostrar una notificación en la barra de notificaciones de Android, incluso si mi aplicación está cerrada.
He intentado buscar, pero no he tenido suerte para encontrar ayuda.
- OpenCV Android: Obtener fotogramas de la cámara en segundo plano sin mostrar en la pantalla
- Up-Sync y Down-Sync en Android?
- Sigue ejecutando una aplicación después de que la pantalla se bloquee automáticamente - Phonegap (Android / iOS)
- La mejor manera de enviar solicitudes HTTP GET ansynchronously en Android?
- Despedir Progress Dialog
Un ejemplo de esto es una aplicación de noticias. Incluso si la pantalla del teléfono está desactivada o la aplicación de noticias está cerrada, todavía puede enviar una notificación de noticias recientes y hacer que aparezca en la barra de notificación.
¿Cómo podría hacer esto en mi propia aplicación?
- Obtener la lista de procesos en ejecución y eliminar sus servicios de fondo
- La aplicación Phonegap muere en segundo plano mientras recoge la ubicación
- En Android, ¿cuál es la diferencia entre los procesos en ejecución y los procesos de fondo en caché?
- ¿Qué sucede con un AsyncTask cuando la actividad de lanzamiento se detiene / destruye mientras todavía se está ejecutando?
- UML: Cómo modelar la interfaz de usuario y los hilos de fondo para aplicaciones móviles (iPhone / iOS, Android)
- ¿Cómo conseguir la localización de los gps una vez para el androide de 5 minutos?
- ¿Cómo puedo mantener mi aplicación en segundo plano?
- ¿Necesita un ejemplo de código sobre cómo ejecutar un servicio Android para siempre en segundo plano incluso cuando el dispositivo está durmiendo, como Whatsapp?
Tienes que construir un Servicio que maneje tus noticias y muestre notificaciones cuando sepa que son nuevas noticias ( Service Doc ). El servicio se ejecutará en segundo plano incluso si su aplicación está cerrada. Necesita un BroadcastReciever para ejecutar el servicio en segundo plano después de completar la fase de arranque. ( Inicie el servicio después del arranque ).
El servicio generará sus notificaciones y las enviará a través de NotificationManager .
EDIT: este artículo puede satisfacer sus necesidades
Utilicé esta respuesta para escribir un servicio y, como ejemplo, necesitas llamar a ShowNotificationIntentService.startActionShow(getApplicationContext())
dentro de una de tus actividades:
import android.app.IntentService; import android.content.Intent; import android.content.Context; public class ShowNotificationIntentService extends IntentService { private static final String ACTION_SHOW_NOTIFICATION = "my.app.service.action.show"; private static final String ACTION_HIDE_NOTIFICATION = "my.app.service.action.hide"; public ShowNotificationIntentService() { super("ShowNotificationIntentService"); } public static void startActionShow(Context context) { Intent intent = new Intent(context, ShowNotificationIntentService.class); intent.setAction(ACTION_SHOW_NOTIFICATION); context.startService(intent); } public static void startActionHide(Context context) { Intent intent = new Intent(context, ShowNotificationIntentService.class); intent.setAction(ACTION_HIDE_NOTIFICATION); context.startService(intent); } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (ACTION_SHOW_NOTIFICATION.equals(action)) { handleActionShow(); } else if (ACTION_HIDE_NOTIFICATION.equals(action)) { handleActionHide(); } } } private void handleActionShow() { showStatusBarIcon(ShowNotificationIntentService.this); } private void handleActionHide() { hideStatusBarIcon(ShowNotificationIntentService.this); } public static void showStatusBarIcon(Context ctx) { Context context = ctx; NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx) .setContentTitle(ctx.getString(R.string.notification_message)) .setSmallIcon(R.drawable.ic_notification_icon) .setOngoing(true); Intent intent = new Intent(context, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, STATUS_ICON_REQUEST_CODE, intent, 0); builder.setContentIntent(pIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notif = builder.build(); notif.flags |= Notification.FLAG_ONGOING_EVENT; mNotificationManager.notify(STATUS_ICON_REQUEST_CODE, notif); } }