I update this notification code @Erel for ~8.1 android . As I test , it works right for 8.1 and 6 device , but works incorrect in Android 4.4.2 (Samsung S3 SPH-L710). The small icon (status bar) is ok but large icon show a blank.
The example code is very simple , create a new project and paste the code to Starter service :
Other , B4A 8.3 and updated with B4A SDK manager , use android-27\android.jar
The example code is very simple , create a new project and paste the code to Starter service :
Other , B4A 8.3 and updated with B4A SDK manager , use android-27\android.jar
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Notification1 As Notification
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("Start")
If Not(Notification1.IsInitialized ) Then
Notification1=CreateNotification("Example","Tap here to set","icon",Main,False,False)
End If
Service.StartForeground(1, Notification1)
End Sub
Sub Service_Destroy
Log("Destroy")
End Sub
Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, _
Sound As Boolean, Vibrate As Boolean) As Notification
Dim p As Phone
If p.SdkVersion >= 21 Then
Dim nb As NotificationBuilder
nb.Initialize
nb.DefaultSound = Sound
nb.DefaultVibrate = Vibrate
nb.ContentTitle = Title
nb.ContentText = Content
nb.setActivity(TargetActivity)
nb.SmallIcon = Icon
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 importance As String
If Sound Then importance = "IMPORTANCE_DEFAULT" Else importance = "IMPORTANCE_LOW"
Dim ChannelVisibleName As String = Application.LabelName
Channel.InitializeNewInstance("android.app.NotificationChannel", _
Array("MyChannelId1", ChannelVisibleName, manager.GetField(importance)))
manager = ctxt.RunMethod("getSystemService", Array("notification"))
manager.RunMethod("createNotificationChannel", Array(Channel))
Dim jo As JavaObject = nb
jo.RunMethod("setChannelId", Array("MyChannelId1"))
End If
Return nb.GetNotification
Else
Dim n As Notification
n.Initialize
n.Sound = Sound
n.Vibrate = Vibrate
n.Icon = Icon
n.SetInfo(Title, Content, TargetActivity)
Return n
End If
End Sub