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
    Code:
    #AdditionalJar: com.android.support:support-v4
    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.

    Attached:
    B4A sample project - zipped
    ProjectRes.zip - 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.

    1.png


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

    #AdditionalJar: com.android.support:support-v4
    #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:
        'Activity.LoadLayout("Layout1")
        nativeMe.InitializeContext
       
        nativeMe.RunMethod(
    "addImage"Array("image6"))
        nativeMe.RunMethod(
    "setId"Array(1))
        nativeMe.RunMethod(
    "setNotificationTitle"Array("Go To B4A Page"))
        nativeMe.RunMethod(
    "setNotificationMessage"Array("Hello B4A"))
        nativeMe.RunMethod(
    "whereToGoTo"Array("https://www.b4x.com/b4a.html"))
        nativeMe.RunMethod(
    "sendNotification"Null)
       
        Sleep(
    2000)
       
        nativeMe.RunMethod(
    "addImage"Array("icon"))
        nativeMe.RunMethod(
    "setId"Array(2))
        nativeMe.RunMethod(
    "setNotificationTitle"Array("Go To B4I Page"))
        nativeMe.RunMethod(
    "setNotificationMessage"Array("Hello B4I"))
        nativeMe.RunMethod(
    "whereToGoTo"Array("https://www.b4x.com/b4i.html"))
        nativeMe.RunMethod(
    "sendNotification"Null)

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    #if Java

    import android.os.Bundle;
    import android.app.NotificationManager;
    import android.support.v4.app.NotificationCompat;
    import android.view.View;
    import android.content.Context;
    import android.net.Uri;
    import android.media.RingtoneManager;
    import android.service.notification.StatusBarNotification;
    import android.service.notification.NotificationListenerService;
    import android.content.Intent;
    import android.app.PendingIntent;


    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) {
             this.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);

            mBuilder.setContentIntent(pendingIntent);
            mBuilder.setSmallIcon(DrawableID);
            mBuilder.setContentTitle(title);
            mBuilder.setSound(alarmSound);
            mBuilder.setContentText(msg);

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

            mNotificationManager.notify(id, mBuilder.build());
        }  
       
        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.
Loading...
  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