19

I am trying to display a notification in the Android notifications bar even if my application is closed.

I've tried searching, but I have had no luck finding help.

An example of this is a news application. Even if the phone screen is off or the news application is closed, it can still send a notification for recent news and have it appear in the notification bar.

How might I go about doing this in my own application?

NickAldwin
  • 11,584
  • 12
  • 52
  • 67
Noob
  • 2,857
  • 6
  • 33
  • 47

3 Answers3

18

You have to build a Service that handles your news and shows notifications when it knows that are new news (Service Doc). The service will run in background even if your application is closed. You need a BroadcastReciever to run the service in background after the boot phase is completed. (Start service after boot).

The service will build your notifications and send them through the NotificationManager.

EDIT: This article may suit your needs

Community
  • 1
  • 1
Idipaolo
  • 788
  • 5
  • 11
3

The selected answer is still correct, but only for devices running Android 7 versions and below. As of Android 8+, you can no longer have a service running in the background while your app is idle/closed.
So, it now depends on how you set up your notifications from your GCM/FCM server. Ensure to set it to the highest priority. If your app is in the background or just not active and you only send notification data, the system process your notification and send it to the Notification tray.

Bamerza
  • 1,335
  • 1
  • 18
  • 34
1

I used this answer to write a service, and as an exmaple you need to call ShowNotificationIntentService.startActionShow(getApplicationContext()) inside one of your activities:

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);
    }
}
Community
  • 1
  • 1
VSB
  • 9,825
  • 16
  • 72
  • 145
  • 2
    The question states he wants to handle notifications even when the app is closed (i.e. - never even started). So, your solution does not solve the problem asked about. – Bamerza Dec 12 '18 at 00:56