Android Question (Solved) Didn't find class "android.app.NotificationChannel"

jose luis gudino

Active Member
Licensed User
Longtime User
Hi everyone, I have the following problem with firebase notification: I have several phones to test my applications , on my phone with version 10 of android and version 9 of android it works correctly, the problem is with my phone that has version 7 of android. appears an error.

the code is as follows: this is the Starter service

B4X:
Sub OBTENCION_IMEI
    Wait For (GetAdvertisingId) Complete (Id As String)
    If Id <> "" Then
        Log(Id)
        mytopic_id = Id.Replace("-","")
        CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
    End If
End Sub

I go back and repeat in version 9 and 10 of android it works perfect, on a phone that I have with version 7 of android I get the following error:

B4X:
firebasemessagingonCreate (java line: 49)
java.lang.NoClassDefFoundError: android.app.NotificationChannel
    at libcore.reflect.InternalNames.getClass(InternalNames.java:55)
    at java.lang.Class.getDexCacheType(Class.java:2551)
    at java.lang.reflect.AbstractMethod.getParameterTypes(AbstractMethod.java:169)
    at java.lang.reflect.Method.getParameterTypes(Method.java:193)
    at java.lang.Class.getDeclaredMethods(Class.java:1812)
    at anywheresoftware.b4a.BA.loadHtSubs(BA.java:522)
    at com.panel.mipaneljll.firebasemessaging.onCreate(firebasemessaging.java:49)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3481)
    at android.app.ActivityThread.-wrap6(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.app.NotificationChannel" on path: DexPathList[[zip file "/data/app/com.panel.mipaneljll-2/base.apk"],nativeLibraryDirectories=[/data/app/com.panel.mipaneljll-2/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at libcore.reflect.InternalNames.getClass(InternalNames.java:53)
    ... 15 more

Thanks in advance
 

jose luis gudino

Active Member
Licensed User
Longtime User
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
Hi
B4X:
Sub Process_Globals
    Public fm As FirebaseMessaging
    Private SIN_NOTIFICAR ="sin_notificar" As String
    Private mytopic As String
    Private smiley As Bitmap
    Dim SQL1 As SQL
End Sub

Sub Service_Create
    mytopic = Starter.mytopic_id
    LogColor("topic firebase: "&mytopic,Colors.Magenta)
    fm.Initialize("fm")
    smiley = LoadBitmapResize(File.DirAssets, "ham04.png", 24dip, 24dip, False)
End Sub

public Sub SubscribeToTopics
    mytopic = Starter.mytopic_id
    LogColor(" ***********  PASE ************** ",Colors.Blue)
    Try   
        fm.SubscribeToTopic(mytopic) 'you can subscribe to more topics
        Dim tiempo = DateTime.Now/100 As Long
        ExecuteRemoteQuery($"call app_fcm ('${mytopic}', 'SERVICE FirebaseMessaging', '${tiempo}', 'SERVICIO (3) registro en FCM')"$,"fmc")
        
    Catch
    End Try
End Sub

public Sub unSubscribeToTopics
    
    mytopic =Starter.mytopic_id
    Try   
        fm.UnsubscribeFromTopic(mytopic) 'you can subscribe to more topics
    Catch
    End Try   
        
End Sub


Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    'Log("Message arrived")
    'Log($"Message data: ${Message.GetData}"$)
    Dim mapa As Map = Message.GetData
    'Log(mapa.Get("notificacion"))
    mqtt_MessageArrived(mapa.Get("notificacion"))
End Sub




Sub fm_TokenRefresh (Token As String)
    LogColor("Token: "&Token,Colors.Red)
End Sub

Sub Service_Destroy

End Sub


Private Sub mqtt_MessageArrived (Payload As String)
  '  ToastMessageShow("llego mensaje",True)
    Dim notificacion As String = Payload
    Dim notificacion_arrivo() As String
    notificacion_arrivo = Regex.Split(",",notificacion)
    'Log(notificacion_arrivo(0))
    HighPriority(notificacion_arrivo(4).Trim,notificacion_arrivo(1).Trim,notificacion_arrivo(2),notificacion_arrivo(9),notificacion_arrivo(2))
    'HighPriority
    ExecuteRemoteQuery($"CALL gts.app_marca_leido('${notificacion_arrivo(0)}','${notificacion_arrivo(1)}','${notificacion_arrivo(5)}','${notificacion_arrivo(2)}')"$,SIN_NOTIFICAR)
    
End Sub

Sub HighPriority(mensaje As String, device As String, id As Int, id_sonido As Int, hora As String)
    
    Dim n As NB6
    'SONIDO 01
    Dim profile As String
    If id_sonido = 1 Then
        Dim FileName As String = "Ringtone1.mp3"
        profile = "Alarmas"
        n.Initialize(profile, "Alarmas", "HIGH",True).AutoCancel(True).SmallIcon(smiley)
        n.Description("Noticaficación de Alarmas MI PANEL")
        
    End If
    If id_sonido = 2 Then
        Dim FileName As String = "Ringtone2.mp3"
        profile = "Eventos"
        n.Initialize(profile, profile, "HIGH",True).AutoCancel(True).SmallIcon(smiley)
        n.Description("Noticaficación de Eventos MI PANEL")
    End If
    File.Copy(File.DirAssets,FileName,Starter.provider.SharedFolder, FileName)
    Dim fecha As String
    DateTime.DateFormat = "dd MMMM"
    DateTime.TimeFormat = "HH:mm"
    fecha= DateTime.Date((hora*1000))&"  "&DateTime.time((hora*1000))
    n.SetDefaults(False, True, True)
    'set custom sound
    n.CustomSound(Starter.Provider.GetFileUri(FileName))
    
    Dim cs As CSBuilder
    n.SubText(cs.Initialize.BackgroundColor(Colors.Transparent).Append($"Hora Evento: ${fecha}"$).PopAll)
    Dim title As Object = cs.Initialize.Color(Colors.RGB(69,67,65)).Append("Panel: "&CargarPaneles(device)).PopAll
    Dim Content As Object = cs.Initialize.Bold.Append($"Evento: ${mensaje}"$ ).PopAll
    Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "Alarm.png", 100dip, 100dip, True)
    n.LargeIcon(largeIcon)
    n.Color(Colors.Red)
    'n.BadgeIconType("SMALL").Number(1)
    n.ShowWhen(DateTime.Now)
    n.Build(title, Content, device, Eventos_04).Notify(id)
    Sleep(1000)
End Sub


Sub ExecuteRemoteQuery(Query As String, JobName As String)
    '*********************************
    '**********************************
    'Starter.LoadData6(Query,JobName)
    '*********************************
    '**********************************
End Sub

Sub CargarPaneles( deviceID As String) As String
    basedatos
    Dim cquery As String
    Dim Cursor1 As Cursor
    cquery=$"SELECT
                panel.nombrepanel
                FROM
                panel
                WHERE
                panel.deviceID = '${deviceID}' "$
                
    Cursor1 = SQL1.ExecQuery(cquery)
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        Dim nombre = Cursor1.GetString("nombrepanel") As String
    Next
    Return nombre
End Sub


Sub basedatos
    
    Dim MyPath As String = File.DirRootExternal & "/Cloud_Alarm"
    Dim MyFolder As String  = "Cloud_Alarm"
    Dim DBFileName As String
    DBFileName = "alarm.sqlite"
    Dim DBFileDir As String
    DBFileDir = MyPath
    Try
        If File.Exists(MyPath, "") =  False  Then
            File.MakeDir(File.DirRootExternal, MyFolder)
        End If
        If File.Exists(DBFileDir, DBFileName) = False  Then
            File.Copy(File.DirAssets, DBFileName, DBFileDir, DBFileName)
        End If
        SQL1.Initialize(DBFileDir,DBFileName,True)
    Catch
    End Try
    
End Sub


#if JAVA
import java.util.ArrayList;
import android.content.Context;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.os.Build;
import java.util.List;
import android.net.Uri;
import anywheresoftware.b4a.objects.collections.Map;

public  List<Object> mGetNotifChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Context context = BA.applicationContext; 
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
             return null;
        }
        List<NotificationChannel> l = notificationManager.getNotificationChannels();
        List<Object> lMap = new ArrayList();
        int i;
        for(i = 0; i < l.size()-1; ++i){
            NotificationChannel channel = l.get(i);
            Object o = getMap(channel);
               lMap.add(o);
        }
        return lMap; 
    }
    return null;
} 

public Object mGetNotifChannel(String channelId){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Context context = BA.applicationContext; 
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
             return null;
        }
        NotificationChannel channel =  notificationManager.getNotificationChannel(channelId);
        if(channel == null){
            return null;
        }
        return getMap(channel);
    }
    return null;
}

public Object getMap(NotificationChannel channel){
    String Id = channel.getId();
    int importance = channel.getImportance();
    int lightcolor = channel.getLightColor();
    Boolean shVibrate = channel.shouldVibrate();
    Boolean shShowLight = channel.shouldShowLights();
    Boolean canShowBadge = channel.canShowBadge();
    Boolean canBypassDnd = channel.canBypassDnd();
    Uri sound = channel.getSound();
    anywheresoftware.b4a.objects.collections.Map map = new anywheresoftware.b4a.objects.collections.Map();
    map.Initialize();
    map.Put("Id", Id);
    map.Put("Sound", sound);
    map.Put("Importance", importance);
    map.Put("Lightcolor", lightcolor);
    map.Put("Vibrate", shVibrate);
    map.Put("ShowLight", shShowLight);
    map.Put("ShowBadge", canShowBadge);
    map.Put("BypassDnd", canBypassDnd);
    return map.getObject();
}
#End If
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
That code was I think I had copied it from another program but I was not really using it, I deleted it and the application worked correctly at once.

once again Thank you for your valuable support
 
Last edited:
Upvote 0
Top