Android Code Snippet Notifications with inline Java code

Discussion in 'Code Snippets' started by Johan Schoeman, May 12, 2018.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Have never done anything with Notifications so I thought I would fiddle around with it this afternoon.

    The only additional library that you need to enable is JavaObject
    The project depends on android-support-v4 - see the reference inside the B4A project
    Nothing to add to the Manifest.

    Once the notifications have been raised and you view your notifications, you can click on the "B4A added notifications" to take you to either the B4A or the B4I web page.

    B4A sample project - zipped - extract it and copy the folder (and its contents) to be on the same folder level as that of the B4A project's /Files and /Objects folders. It contains 2 png files.


    Sample Code:
    #Region  Project Attributes
    #ApplicationLabel: JHSnotification
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    #End Region

    #AdditionalRes: ..\ProjectRes

    'Tested on Android 4.4.2 and Android 7.0 devices

    '#Extends: android.service.notification.NotificationListenerService

    #Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: 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 nativeMe As JavaObject

    End Sub

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    "setNotificationTitle"Array("Go To B4A Page"))
    "setNotificationMessage"Array("Hello B4A"))
    "setNotificationTitle"Array("Go To B4I Page"))
    "setNotificationMessage"Array("Hello B4I"))

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    #if Java

    import android.os.Bundle;
    import android.view.View;
    import android.content.Context;
    import android.service.notification.StatusBarNotification;
    import android.service.notification.NotificationListenerService;
    import android.content.Intent;

    public int DrawableID = -1;
    public int id = -1;
    public String msg = "";
    public String title = "";
    public NotificationManager mNotificationManager;
    public String wheretogoto = "";

        public void addImage(String draw) {
             int DrawableID = this.getResources().getIdentifier(draw, "drawable", BA.packageName);
             this.DrawableID = DrawableID;
        public void setId(int id) {
    = id;
        public void setNotificationMessage(String msg) {
             this.msg = msg;
        public void setNotificationTitle(String title) {
             this.title = title;
        public void sendNotification() {

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(wheretogoto));
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


            mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            this.mNotificationManager = mNotificationManager;

        public void whereToGoTo (String wheretogoto) {
            this.wheretogoto = wheretogoto;

    #End If
    Probably not useful at all - there are a number of Notification Libs in the forum...add some additional inline Java code to make it more "useful" should you wish to do so.

    Have tested it on Android 4.4.2 and Android 7.0 devices and working on both

    Attached Files:

  2. Star-Dust

    Star-Dust Expert Licensed User

    Very good :D
    Johan Schoeman likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    peacemaker and Johan Schoeman like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice