Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android notification in background if app closed?

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?

like image 996
Noob Avatar asked Sep 15 '13 12:09

Noob


People also ask

Do local notifications work when app is closed?

Current Behavior. A local notification is schedule at a given time. The notification only happens if the app is open or in the foreground. If the app is closed the notification does not happen (no notification card).

Does an app need to be open for push notifications?

Push notifications are small, pop-up messages sent to a user's device by a mobile app that appear even when the app isn't open. These notifications are designed to grab attention and can convey reminders, updates, promotions, and more. Push notifications can consist of a title, a message, an image, and a URL.

How do I know if apps are running in the background Android?

If you don't want the app to relaunch when you restart your phone, tap Uninstall to remove the app. To see what apps are running in the background, go to Settings > Developer Options > Running Services.


3 Answers

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

like image 195
Idipaolo Avatar answered Sep 26 '22 06:09

Idipaolo


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.

like image 45
Bamerza Avatar answered Sep 26 '22 06:09

Bamerza


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);
    }
}
like image 21
VSB Avatar answered Sep 22 '22 06:09

VSB