How to show an app badge in Oreo, when app is closed (not running at all)?

Raj Kumar Singh
        public class GcmBroadcastReceiver extends BroadcastReceiver
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {// Control comes here when any new notification is received
                 final Notification notification = getNotification(threadId, jobTitle, body, subTitle, title, imageURL);
                EventLoggerService.showAppBadge(context, notification, new EventLoggerService.EventLoggerServiceStartedListner(){
                    @Override
                    public void eventLoggerServiceStarted() {
                        notificationManager.notify(101, notification);
                    }
                });
            }

            public Notification getNotification(final String threadId, String jobTitle, final String body ,String subTitle, String title, final String imageURL)
               {
                    final Context context = CareSDKApplication.singleton().getApplicationContext();
                    mThreadId = threadId;
                    mImageURL = imageURL;
                    mBody = body;
                    mTitle = title;
                    mSubTitle = subTitle;

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationsUtils.NOTIFICATION_CHANNEL_ID)
                        // .setLargeIcon(largeIcon)
                        .setSmallIcon(R.drawable.message_notification)
                        .setAutoCancel(true)
                        .setContentIntent(getContentIntent(threadId))
                        .setTicker(getTicker(threadId));

                    RemoteViews customNotification = new RemoteViews(context.getPackageName(), R.layout.image_notification_custom_view);
                    builder.setCustomContentView(customNotification);
                    if (!TextUtils.isEmpty(title)) 
                {
                        customNotification.setTextViewText(R.id.title, StringEscapeUtils.unescapeJava(title));
                            customNotification.setTextViewText(R.id.sub_title,  StringEscapeUtils.unescapeJava(subTitle));
                        customNotification.setTextViewText(R.id.content, StringEscapeUtils.unescapeJava(body));
                    }
                    else
                   {
                        customNotification.setTextViewText(R.id.title, "");
                        customNotification.setTextViewText(R.id.sub_title,  StringEscapeUtils.unescapeJava(body));
                        customNotification.setTextViewText(R.id.content, "");
                }
                customNotification.setTextViewText(R.id.time, new SimpleDateFormat("HH:mm").format(new Date()));
                customNotification.setTextViewText(R.id.number_of_notifications, getUnreadCount(threadId) + "");
                //builder.setPriority(Notification.PRIORITY_MAX);


                mBuilder = builder;

                // For versions equal and higher than jelly bean
                if (Build.VERSION.SDK_INT >= 16) {
                    builder.setStyle(getStyle(threadId));
                }

                if (!TextUtils.isEmpty(imageURL)) {
                    mBigPicture = Utils.getImage(context, imageURL, this);

                    if (mBigPicture != null) {
                        setImageNotification(builder, context, threadId, body, title, subTitle);
                    }
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder.setColor(0x00bce4);
                }
                Notification notification = builder.build();

                return notification;
            }
        }


        public class EventLoggerService extends IntentService
        {
            private static EventLoggerServiceStartedListner 
            mEventLoggerServiceStartedListner;
            private static Notification mNotification;

            @Override
            public void onCreate()
            {
                super.onCreate();
                // This is required for Android O
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1)
                {
                    android.os.Debug.waitForDebugger();
                    NotificationsUtils notificationsUtils = new NotificationsUtils(this);
                    startForeground(1, mNotification);
                    Log.d("EventLoggerService", "EventLoggerService Created");
                }
            }

            public interface EventLoggerServiceStartedListner{
                public void eventLoggerServiceStarted();
            }
            @Override
            public void onStart(@Nullable Intent intent, int startId) {
                super.onStart(intent, startId);
                Log.d("EventLoggerService", "EventLoggerService Started");
            }

            @Override
            public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
                super.onStartCommand(intent, flags, startId);
                Log.d("EventLoggerService", "EventLoggerService onStartCommand");
                mEventLoggerServiceStartedListner.eventLoggerServiceStarted();
                return START_STICKY;
            }

            public static void showAppBadge(Context context, Notification notification, EventLoggerServiceStartedListner eventLoggerServiceStartedListner)
            {
                android.os.Debug.waitForDebugger();
                mNotification = notification;
                mEventLoggerServiceStartedListner = eventLoggerServiceStartedListner;
                Intent service = new Intent(context, EventLoggerService.class);
                context.startForegroundService(service);
            }

        }



        public class NotificationsUtils extends ContextWrapper{
            public static final String NOTIFICATION_CHANNEL_ID = "Care_push_note";
            public static final String NOTIFICATION_CHANNEL_NAME = "Care.com";

            Context mContext;
            NotificationManager mNotificationManager;
            public NotificationsUtils(Context context) {
                super(context);
                mContext = context;
                createChannels();
            }

            public void createChannels()
            {
    // Notification Channel is created for same channel id as Notification is created in side getNotification method of GcmBroadcastReceiver 
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                channel.setShowBadge(true);
                channel.enableLights(true);
                channel.enableVibration(true);
                channel.setLightColor(Color.GREEN);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                getManager().createNotificationChannel(channel);
            }

            public NotificationManager getManager() {
                if (mNotificationManager == null) {
                    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                }
                return mNotificationManager;
            }
        }

All method calls are executing as expected, geting Notification in Notification bar but Appbadge is not shown in Android O devices, when app is closed. Please tell me what is the mistake I am doing?

If I put Channel creation code and Notification code in side click event of some view then it works fine, when app is open.

Demigod

In Android notification badges doesn't work as they are in iOS. They just showing that the app has some notifications that are in the status bar. When notification is removed from status bar, than badge will also disappear.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Show notifications when app is closed

From Dev

How to keep an IntentService running even when app is closed?

From Dev

How to show a notification everyday at a certain time even when the app is closed?

From Java

Android. Is WorkManager running when app is closed?

From Dev

How to keep service running in background when app is closed but stop/disable it when the app is in foreground?

From Dev

How to keep a CountDownTimer running even if the app is closed?

From Dev

show dialog on specific time ether the app is running or closed

From Dev

How to clear notification badge count when reinstall the app

From Dev

Show Badge count on App icon in Android

From Dev

How to stop Android service when app is closed

From Dev

how have android notifications when an app is closed

From Dev

How to detect a broadcast receiver when app is closed?

From Dev

Android How To Send Notifications When App Is Closed

From Dev

How to handle localNotifications when app is closed

From Dev

Keeping a Java app running when SSH is closed on Google Cloud

From Dev

Android Studio: Countdown timer which is also running when the app is closed

From Dev

Correct way to show ViewController from push notification when app is closed

From Dev

App closes when RewardedVideoAd is closed. Show Fragment onAdClose?

From Dev

If a Java app is running as a service how can the Spring context be closed?

From Dev

get data from push notification when app is closed in iOS and app not running in background

From Dev

How to close all running app programmatically in android?

From Dev

cancel AlarmManager when app is closed

From Dev

Receive push when app is closed

From Dev

CountDownTimer problem when app is closed

From Dev

CountDownTimer problem when app is closed

From Dev

Make notification when app is closed

From Dev

Notification null when app closed

From Dev

How to reset the Badge app icon number?

From Dev

Badge iOS application without running the app or sending a push notification?

Related Related

  1. 1

    Show notifications when app is closed

  2. 2

    How to keep an IntentService running even when app is closed?

  3. 3

    How to show a notification everyday at a certain time even when the app is closed?

  4. 4

    Android. Is WorkManager running when app is closed?

  5. 5

    How to keep service running in background when app is closed but stop/disable it when the app is in foreground?

  6. 6

    How to keep a CountDownTimer running even if the app is closed?

  7. 7

    show dialog on specific time ether the app is running or closed

  8. 8

    How to clear notification badge count when reinstall the app

  9. 9

    Show Badge count on App icon in Android

  10. 10

    How to stop Android service when app is closed

  11. 11

    how have android notifications when an app is closed

  12. 12

    How to detect a broadcast receiver when app is closed?

  13. 13

    Android How To Send Notifications When App Is Closed

  14. 14

    How to handle localNotifications when app is closed

  15. 15

    Keeping a Java app running when SSH is closed on Google Cloud

  16. 16

    Android Studio: Countdown timer which is also running when the app is closed

  17. 17

    Correct way to show ViewController from push notification when app is closed

  18. 18

    App closes when RewardedVideoAd is closed. Show Fragment onAdClose?

  19. 19

    If a Java app is running as a service how can the Spring context be closed?

  20. 20

    get data from push notification when app is closed in iOS and app not running in background

  21. 21

    How to close all running app programmatically in android?

  22. 22

    cancel AlarmManager when app is closed

  23. 23

    Receive push when app is closed

  24. 24

    CountDownTimer problem when app is closed

  25. 25

    CountDownTimer problem when app is closed

  26. 26

    Make notification when app is closed

  27. 27

    Notification null when app closed

  28. 28

    How to reset the Badge app icon number?

  29. 29

    Badge iOS application without running the app or sending a push notification?

HotTag

Archive