0

Im making a SoftPhone App, and right now im facing the issue that my Service Notification is not enabled by default is there a way to enable it by default or ask the user for permission to enable it without making an intent to access settings?

Edit:

My service in my manifest:

<service
       android:name=".Service"
       android:label="PBXService"/>

I start this service like this:

startService(new Intent(ACTION_MAIN).setClass(this, Service.class));

Inside the service i show the service notification like this:

mNotif = Compatibility.createNotification(this, mNotificationTitle, "Phone service", R.mipmap.ic_launcher, R.mipmap.ic_launcher, bm, mNotifContentIntent, true, notifcationsPriority);
//mNM is my notification manager.
mNM.notify(id, mNotif);

Compatibility class method:

public static Notification createSimpleNotification(Context context, String title, String text, PendingIntent intent) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createSimpleNotification(context, title, text, intent);
        } else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
            return ApiTwentyOnePlus.createSimpleNotification(context, title, text, intent);
        } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
            return ApiSixteenPlus.createSimpleNotification(context, title, text, intent);
        } else {
            return ApiElevenPlus.createSimpleNotification(context, title, text, intent);
        }
    }

In Api26+ Class:

public static Notification createSimpleNotification(Context context, String title, String text, PendingIntent intent) {
        Notification notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.linphone_logo)
                .setAutoCancel(true)
                .setContentIntent(intent)
                .setDefaults(Notification.DEFAULT_SOUND
                        | Notification.DEFAULT_VIBRATE)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setVisibility(Notification.VISIBILITY_PRIVATE)
                .setPriority(Notification.PRIORITY_HIGH)
                .setWhen(System.currentTimeMillis())
                .setShowWhen(true)
                .setColorized(true)
                .setColor(context.getColor(R.color.notification_color_led))
                .build();

        return notif;
    }

Phone model: SM-J737A Android version: 8.0.0

I know for a fact that this works because whenever the permission is manually activated i have no issues with this.

ivan
  • 1,177
  • 8
  • 23
  • AFAIK you don't have to ask permissions for Service Notification since it's not dangerous - https://developer.android.com/guide/topics/permissions/overview#normal-dangerous – HB. Aug 13 '19 at 17:15
  • You only have to declare the service in your manifest – HB. Aug 13 '19 at 17:16
  • Have a look at this answer- https://stackoverflow.com/a/42452341/5550161 – HB. Aug 13 '19 at 17:18
  • @HB. It is declared on manifest but when i install the app its turned off by default. It also works but it does not show the notification and that's a problem because it only says "X app is using the battery" Which often leads to the user shutting it down. – ivan Aug 13 '19 at 17:26
  • Also the link you just pointed me towards is an answer that does open up the settings app. – ivan Aug 13 '19 at 17:28
  • The second link was for demonstration purposes.. Since you haven't provided any info about what you have implemented, I can't help you. – HB. Aug 13 '19 at 17:30
  • @HB. Got it will provide some example of what i currently have. – ivan Aug 13 '19 at 17:35
  • @HB. Question updated. – ivan Aug 13 '19 at 17:45
  • Could you please add the code for `Compatibility` class? Also mention which device and android version you're testing on. – Birju Vachhani Aug 19 '19 at 05:08
  • @BirjuVachhani Code for compatibility class added. Android version 8.0.0, Device: Samsung J7 – ivan Aug 20 '19 at 15:25
  • @ivan Well, the `Compatibility` doesn't help much as all it does is to call another method. Please make sure that you are creating notification channel before you try to show notification for Android 8.0+ – Birju Vachhani Aug 21 '19 at 05:53
  • @BirjuVachhani the thing is that the notification is sent without problem at all, but the setting to allow it to appear is initially disabled, is there any way to change this? (Also added the method used) – ivan Aug 21 '19 at 15:07
  • @ivan when install an app for the first time and create notification channel then the settings will be enabled by default unless you manually turn them off. There might be some issue if it is custom ROM and it is highly modified. – Birju Vachhani Aug 21 '19 at 18:12
  • @BirjuVachhani Sorry for the late reply but the notifications are enabled by default but the service isn't. – ivan Aug 26 '19 at 17:30

0 Answers0