Android Question Notifications stopped working (targetSdkVersion=26)

martin24

Member
Licensed User
If I use the following in my manifest:

B4X:
...
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="25"/>
...
AddPermission(android.permission.VIBRATE)

Then the following notification works without problems:

B4X:
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.SetInfo("This is the title", "and this is the body.", "") 'Change Main to "" if this code is in the main module.
n.Notify(1)

When I change to android:targetSdkVersion="26", then the notification is no longer displayed. I have also tried the NotificationBuilder with the same effect.

What do I miss here?
 

Star-Dust

Expert
Licensed User
Longtime User
How can you solve?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
How do you solve keeping SDK_Target at 26?
Because we have to adapt to the requirements for future prosism.
What is the solution
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
How do you solve keeping SDK_Target at 26?
Because we have to adapt to the requirements for future prosism.
What is the solution
There's no problem still targeting SDK 22, and to get the notification libraries to work on SDK 26+ they will have to be updated to handle "Notification channels".
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Relevant tutorial: https://www.b4x.com/android/forum/t...etsdkversion-minsdkversion.87610/#post-554210

You can use this code together with NotificationBuilder library to create a notification and set its channel:
B4X:
Sub Button1_Click
   Dim nb As NotificationBuilder
   nb.Initialize
   nb.SmallIcon = "icon"
   nb.ContentTitle = "title"
   nb.ContentText = "content"
   Dim p As Phone
   If p.SdkVersion >= 26 Then
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       Dim manager As JavaObject
       manager.InitializeStatic("android.app.NotificationManager")
       Dim Channel As JavaObject
       Dim ChannelVisibleName As String = Application.LabelName
       Channel.InitializeNewInstance("android.app.NotificationChannel", _
           Array("MyChannelId1", ChannelVisibleName, manager.GetField("IMPORTANCE_DEFAULT"))) 'change to IMPORTANCE_LOW to remove sounds
       manager = ctxt.RunMethod("getSystemService", Array("notification"))
       manager.RunMethod("createNotificationChannel", Array(Channel))
       Dim jo As JavaObject = nb
       jo.RunMethod("setChannelId", Array("MyChannelId1"))
   End If
   nb.Notify(1)
End Sub
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thanks, I will use the code you posted.

Maybe I'm worried too much about the changes. But since I stayed a while back in the last have, I have the anxiety to recover the knowledge that I miss to get to SDK 26
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Relevant tutorial: https://www.b4x.com/android/forum/t...etsdkversion-minsdkversion.87610/#post-554210

You can use this code together with NotificationBuilder library to create a notification and set its channel:
B4X:
Sub Button1_Click
   Dim nb As NotificationBuilder
   nb.Initialize
   nb.SmallIcon = "icon"
   nb.ContentTitle = "title"
   nb.ContentText = "content"
   Dim p As Phone
   If p.SdkVersion >= 26 Then
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       Dim manager As JavaObject
       manager.InitializeStatic("android.app.NotificationManager")
       Dim Channel As JavaObject
       Dim ChannelVisibleName As String = "My Channel"
       Channel.InitializeNewInstance("android.app.NotificationChannel", _
           Array("MyChannelId1", ChannelVisibleName, manager.GetField("IMPORTANCE_DEFAULT")))
       manager = ctxt.RunMethod("getSystemService", Array("notification"))
       manager.RunMethod("createNotificationChannel", Array(Channel))
       Dim jo As JavaObject = nb
       jo.RunMethod("setChannelId", Array("MyChannelId1"))
   End If
   nb.Notify(1)
End Sub

This code should also be added to the original notifications tutorial/post, or at least a link from the post above to the original tutorial/post.
 
Upvote 0
Top