startForeground fail after upgrade to Android 8.1

After some tinkering for a while with different solutions i found out that one must create a notification channel in Android 8.1 and above. private fun startForeground() { val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(“my_service”, “My Background Service”) } else { // If earlier version channel ID is not used // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) “” … Read more

Android 4.1: How to check notifications are disabled for the application?

You can’t 100% can’t. It is asked in this Google I/O 2012 video and the Project lead for the new notifications declares that you can’t. Edit 2016 update: Now you can check it, as said in this Google I/O 2016 video. Use NotificationManagerCompat.areNotificationsEnabled(), from support library, to check if notifications are blocked on API 19+. … Read more

Custom notification layouts and text colors

The solution is to use built-in styles. The style you need is called TextAppearance.StatusBar.EventContent in Android 2.3 and Android 4.x. In Android 5.x material notifications use several other styles: TextAppearance.Material.Notification, TextAppearance.Material.Notification.Title, and TextAppearance.Material.Notification.Line2. Just set the appropriate text appearance for the text view, and you will get the necessary colors. If you are interested how … Read more

Change Notification Layout

First create an xml for your notification. custom_notification.xml: <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/layout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:padding=”10dp” > <ImageView android:id=”@+id/image” android:layout_width=”wrap_content” android:layout_height=”fill_parent” android:layout_alignParentLeft=”true” android:layout_marginRight=”10dp” /> <TextView android:id=”@+id/title” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_toRightOf=”@id/image” style=”Custom Notification Title” /> <TextView android:id=”@+id/text” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_toRightOf=”@id/image” android:layout_below=”@id/title” style=”Custom Notification Text” /> </RelativeLayout> Now the java code: public class MainActivity extends Activity { @SuppressWarnings(“deprecation”) @Override public … Read more

Create a notification

I think you want to implement firebase to your app to show notifications. use push-notifications. steps: add firebase to your android project . you can see the step by step here. https://firebase.google.com/docs/android/setup the next step is to read and write data to firebase. https://firebase.google.com/docs/database/android/read-and-write here when you read the data you can push notification. if … Read more