So, i have an app on store with 50000 users. Recently i updated my android studio and compiled SDK to 27. I made some changes to support android 8 changes including Notification
I just just solved this problem in my application, what happened is that in the class that used my notifications, I kept the CHANNEL_ID and CHANNEL_NAME of the NotificationChannel in a String and Constant, Static or Final variable, but I moved those values to the XML Resource.String since they are values that I never change and I get them with GetString (Resource.String.CH_NAME_GPS_ERROR_ID) and I don't know why but it worked, I hope this also solves your problem
Yes, there is a way you can find out which class is throwing this error.
So, first of all, you need to find out which class is calling Service.startForeground
. That class could be throwing it.
Using Android Studio, go to the source code for Service
class and find the startForeground
method. Hold ctrl
(or Command
on Mac) and click on the method signature. It will list classes from all libs that are calling that method, including documentation references.
I'm still facing this same problem. I think RoboSpice (which is archived now) is throwing these errors. The only way to find out is to refactor all codebase to use something else (Rx+Retrofit maybe).
You forgot to set channel id for the notification.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) builder.setChannelId(youChannelID);
The if
statement is optional and depends on your Min SDK level.
if you use service class you can put this code
public class BlablaService extends Service
@Override
public void onCreate(){
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_1)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
Of course don not forget ForeGroundService Permission your AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
it appears that you in 8.1 must create your own notification channel.
private void startForeground() {
String channelId ="";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel("my_service", "My Background Service");
}else{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(101, notification);
}
}
and after
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId ,String channelName){
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(chan);
return channelId;
}
Update: Also don't forget to add the foreground permission as required Android P:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />