Estimote / Android-Proximity-SDK

Estimote Proximity SDK for Android
https://developer.estimote.com
MIT License
83 stars 41 forks source link

Background Monitor Notification Crash Android O #71

Closed ldimitroff closed 6 years ago

ldimitroff commented 6 years ago

Prerequisites

Basic information

Estimote SDK version: [0.6.2]

Android devices affected: [Android 8.1]

Android OS version affected: [Oreo 8.1]

Beacon hardware version: [No need]

Description

Using the code described I can't start the monitoring service. I get the crash

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=WB_BEACON_CHANNEL pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6753) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

ldimitroff commented 6 years ago

For android O, there's the need to create a Notification Channel like this:

private static 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) {
       NotificationChannel channel = new NotificationChannel(CHANNEL_ID, >"BEACON_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH);
       channel.setDescription("BEACON_CHANNEL_DESCRIPTION");
       // Register the channel with the system; you can't change the importance
       // or other notification behaviors after this
       NotificationManager notificationManager = >getContext().getSystemService(NotificationManager.class);
       if (notificationManager != null) {
           notificationManager.createNotificationChannel(channel);
       }
   }

}

pawelDylag commented 6 years ago

Hey @ldimitroff

As I understand, you've found a solution for your problem. Indeed, you need to setup your notification object properly according to the OS version.