3

When I long press home button and kill my app, the notification was removed from status bar, but I want keep it when app killed, how to keep notification on status bar when app killed?

Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
galaxy bruce
  • 175
  • 1
  • 2
  • 7

3 Answers3

0

What u can do is use:

startForeground()

Note: You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.

Example 1

Example2

And to stop this use:

stopForeground()

Note: Remove this service from foreground state, allowing it to be killed if more memory is needed.

Hope this helps u out.

Community
  • 1
  • 1
Strider
  • 4,452
  • 3
  • 24
  • 35
0

Create a method like below in activity or fragment i.e where you want notification service. And then call this method there.

private void buildNotification(){

    Intent intent = new Intent(getActivity(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

    AlarmManager am = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
}

Now create a class (here I created NotificationReceiver.class) that extends BroadcastReceiver as below.

public class NotificationReceiver extends BroadcastReceiver{

private static final int NOTIFICATION_ID = 1991;

@Override
public void onReceive(Context context, Intent intent) {

    int num = 0;

    Intent contentIntent = new Intent(context, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_notifications_black_24dp);
    builder.setContentTitle("your title.");
    builder.setContentText("your text.");
    builder.setContentIntent(pendingIntent);
    builder.setTicker("ticker text.");
    builder.setAutoCancel(true);
    builder.setOnlyAlertOnce(true);
    builder.setDefaults(Notification.DEFAULT_ALL);
    builder.setNumber(++num);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

This will solve your problem.

Update

I forget to mention that you also have to add receiver in AndroidManifest.xml.

<receiver android:name=".NotificationReceiver"/>

i.e. you have to put your class file in receiver

dawanse
  • 149
  • 3
  • 14
0

Your notification needs to be owned by something that persists after the activity I'd closed, like a Service. Create a small Service that manages your notification. Your activities can send intents to the service, or bind to it and invoke methods directly through a connection, to modify the state of the notification.

I'm on mobile so can't easily construct an example... Stay tuned. ^_^

zerobandwidth
  • 1,213
  • 11
  • 18