Background service is not working in Oreo

前端 未结 3 701
后悔当初
后悔当初 2020-12-01 11:43

I want to run my app in background if I kill the app instance also. But after I kill my app the service also stops working. Here is my code please any one help me to solve m

3条回答
  •  再見小時候
    2020-12-01 12:16

    Oreo Introduced

    new Concept PIP (Picture in Picture Mode ) and it have categories services control by making channels and priority to them.you have to change the code just for oreo to create notifications and services

    read about google developers documentation carefully here https://developer.android.com/guide/topics/ui/notifiers/notifications both java and kotlin code is available here to create notification in oreo

    https://developer.android.com/training/notify-user/build-notification

    it was my effort to find the solution after searching and sharing with you.

    here is some sample code :

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    

    for creating channels write this code:

    private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        }
    }
    

    you can see full detials for push notifications and sending messages by clicking on the above links.

提交回复
热议问题