Android Code Snippet Notifications with inline Java code

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
B4X:
#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:
B4X:
#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
 

Attachments

  • ProjectRes.zip
    44.9 KB · Views: 220
  • b4aJHSnotification.zip
    8.7 KB · Views: 207

Star-Dust

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
B4X:
#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.

View attachment 67804


Sample Code:
B4X:
#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
Very good :D
 
Top