foreground service for persistent notification









up vote
1
down vote

favorite












I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.



My code:



 void setUpAsForeground(String text) 
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);



any advice would be highly appreciated.










share|improve this question









New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Can you describe "is not working"?
    – Laurenz Albe
    Nov 9 at 13:51










  • @LaurenzAlbe yes, it is simply not showing the notification when I run the application.
    – John Doe
    Nov 9 at 13:54














up vote
1
down vote

favorite












I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.



My code:



 void setUpAsForeground(String text) 
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);



any advice would be highly appreciated.










share|improve this question









New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Can you describe "is not working"?
    – Laurenz Albe
    Nov 9 at 13:51










  • @LaurenzAlbe yes, it is simply not showing the notification when I run the application.
    – John Doe
    Nov 9 at 13:54












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.



My code:



 void setUpAsForeground(String text) 
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);



any advice would be highly appreciated.










share|improve this question









New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.



My code:



 void setUpAsForeground(String text) 
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);



any advice would be highly appreciated.







java android api deprecated






share|improve this question









New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 9 at 13:57









Kling Klang

32k156286




32k156286






New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 9 at 13:39









John Doe

61




61




New contributor




John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






John Doe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Can you describe "is not working"?
    – Laurenz Albe
    Nov 9 at 13:51










  • @LaurenzAlbe yes, it is simply not showing the notification when I run the application.
    – John Doe
    Nov 9 at 13:54
















  • Can you describe "is not working"?
    – Laurenz Albe
    Nov 9 at 13:51










  • @LaurenzAlbe yes, it is simply not showing the notification when I run the application.
    – John Doe
    Nov 9 at 13:54















Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51




Can you describe "is not working"?
– Laurenz Albe
Nov 9 at 13:51












@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54




@LaurenzAlbe yes, it is simply not showing the notification when I run the application.
– John Doe
Nov 9 at 13:54












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification






share|improve this answer



























    up vote
    0
    down vote













    Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
    If all you need is to make a Notification from your app then you consider using this code.



     public class makeNotification 
    private static final String ChannelId = "ChannelId";
    private static final String CHANNEL_ID = "cahhnel";
    private static final int NOTIFICATION_ID = 99;

    public static void makenotification(Context context)
    Intent intent = new Intent(context,DemoVolleyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
    NotificationManager notificationManager = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    NotificationChannel mChannel = new NotificationChannel(
    CHANNEL_ID,
    "Channel Name",
    NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(mChannel);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
    .setSmallIcon(R.drawable.ic_lock_black_24dp)
    .setContentTitle("My Notification")
    .setContentText("notification_body")
    .setDefaults(Notification.DEFAULT_VIBRATE)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
    && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
    notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());







    share|improve this answer








    New contributor




    Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • That worked, thank you.
      – John Doe
      Nov 9 at 15:38










    • Alright Good to know it worked
      – Charlyge
      Nov 9 at 15:52










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    John Doe is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226811%2fforeground-service-for-persistent-notification%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification






    share|improve this answer
























      up vote
      1
      down vote













      Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification






        share|improve this answer












        Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 14:22









        Larry Schiefer

        13.4k11829




        13.4k11829






















            up vote
            0
            down vote













            Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
            If all you need is to make a Notification from your app then you consider using this code.



             public class makeNotification 
            private static final String ChannelId = "ChannelId";
            private static final String CHANNEL_ID = "cahhnel";
            private static final int NOTIFICATION_ID = 99;

            public static void makenotification(Context context)
            Intent intent = new Intent(context,DemoVolleyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            NotificationChannel mChannel = new NotificationChannel(
            CHANNEL_ID,
            "Channel Name",
            NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_lock_black_24dp)
            .setContentTitle("My Notification")
            .setContentText("notification_body")
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());







            share|improve this answer








            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • That worked, thank you.
              – John Doe
              Nov 9 at 15:38










            • Alright Good to know it worked
              – Charlyge
              Nov 9 at 15:52














            up vote
            0
            down vote













            Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
            If all you need is to make a Notification from your app then you consider using this code.



             public class makeNotification 
            private static final String ChannelId = "ChannelId";
            private static final String CHANNEL_ID = "cahhnel";
            private static final int NOTIFICATION_ID = 99;

            public static void makenotification(Context context)
            Intent intent = new Intent(context,DemoVolleyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            NotificationChannel mChannel = new NotificationChannel(
            CHANNEL_ID,
            "Channel Name",
            NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_lock_black_24dp)
            .setContentTitle("My Notification")
            .setContentText("notification_body")
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());







            share|improve this answer








            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • That worked, thank you.
              – John Doe
              Nov 9 at 15:38










            • Alright Good to know it worked
              – Charlyge
              Nov 9 at 15:52












            up vote
            0
            down vote










            up vote
            0
            down vote









            Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
            If all you need is to make a Notification from your app then you consider using this code.



             public class makeNotification 
            private static final String ChannelId = "ChannelId";
            private static final String CHANNEL_ID = "cahhnel";
            private static final int NOTIFICATION_ID = 99;

            public static void makenotification(Context context)
            Intent intent = new Intent(context,DemoVolleyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            NotificationChannel mChannel = new NotificationChannel(
            CHANNEL_ID,
            "Channel Name",
            NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_lock_black_24dp)
            .setContentTitle("My Notification")
            .setContentText("notification_body")
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());







            share|improve this answer








            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel
            If all you need is to make a Notification from your app then you consider using this code.



             public class makeNotification 
            private static final String ChannelId = "ChannelId";
            private static final String CHANNEL_ID = "cahhnel";
            private static final int NOTIFICATION_ID = 99;

            public static void makenotification(Context context)
            Intent intent = new Intent(context,DemoVolleyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            NotificationChannel mChannel = new NotificationChannel(
            CHANNEL_ID,
            "Channel Name",
            NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_lock_black_24dp)
            .setContentTitle("My Notification")
            .setContentText("notification_body")
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());








            share|improve this answer








            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 9 at 15:36









            Charlyge

            11




            11




            New contributor




            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Charlyge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.











            • That worked, thank you.
              – John Doe
              Nov 9 at 15:38










            • Alright Good to know it worked
              – Charlyge
              Nov 9 at 15:52
















            • That worked, thank you.
              – John Doe
              Nov 9 at 15:38










            • Alright Good to know it worked
              – Charlyge
              Nov 9 at 15:52















            That worked, thank you.
            – John Doe
            Nov 9 at 15:38




            That worked, thank you.
            – John Doe
            Nov 9 at 15:38












            Alright Good to know it worked
            – Charlyge
            Nov 9 at 15:52




            Alright Good to know it worked
            – Charlyge
            Nov 9 at 15:52










            John Doe is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            John Doe is a new contributor. Be nice, and check out our Code of Conduct.












            John Doe is a new contributor. Be nice, and check out our Code of Conduct.











            John Doe is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226811%2fforeground-service-for-persistent-notification%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo