Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification

@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);
}
Comment

PREVIOUS NEXT
Code Example
Java :: gat environment variables java 
Java :: delete one item from list recycleview 
Java :: android textview center align text programmatically 
Java :: HUFFMAN CODING IN JAVA 
Java :: hide steam games from friends 
Java :: calculate smallest angle difference 
Java :: android java get value from listview item 
Java :: for and while loops java 
Java :: use regex in if statement java 
Java :: dictionary in java 
Java :: android ecode base64 
Java :: cors spring 
Java :: file java class 
Java :: java instanceof 
Java :: convert array of char to string java 
Java :: java types of list 
Java :: difference between class and interface 
Java :: java arraylist add 
Java :: override class java 
Java :: java actionevent 
Java :: enum values to string array 
Java :: what is jvm jdk and jre 
Java :: java string character at index 
Java :: java difference hashmap hashtable 
Java :: how to find sum of the digit of the numbers in java 
Java :: mockito method called 
Java :: anagram java 
Java :: java parse json to class 
Java :: Java get color from rgb values 
Java :: can i call another function from main hava 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =