Recent

Author Topic: [LAMW] jNotificationManager is unable to make silent notification  (Read 216 times)

Segator

  • Full Member
  • ***
  • Posts: 164
    • https://github.com/Nenirey
Hi @jmpessoa, actualy i make the silent notification with a modification to the jNotificationManager.java:
Code: Pascal  [Select][+][-]
  1. package com.example.appnotificationmanagerdemo1;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.Random;
  5.  
  6. import android.app.Notification;
  7. import android.app.NotificationChannel;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.os.Build;
  13. import android.util.Log;
  14.  
  15. /*Draft java code by "Lazarus Android Module Wizard" [2/3/2015 16:12:53]*/
  16. /*https://github.com/jmpessoa/lazandroidmodulewizard*/
  17. /*jControl template*/
  18.  
  19. public class jNotificationManager /*extends ...*/ {
  20.  
  21.     private long     pascalObj = 0;      // Pascal Object
  22.     private Controls controls  = null;   // Control Class -> Java/Pascal Interface ...
  23.     private Context  context   = null;
  24.    
  25.     String one,two,three;
  26.     NotificationManager mNotificationManager;        
  27.     Notification.Builder mNotificationBuilder;
  28.     Notification mNotification;    //https://www.laurivan.com/android-notifications-with-custom-layout/
  29.  
  30.     int mPendingFlag = PendingIntent.FLAG_CANCEL_CURRENT;  //FLAG_UPDATE_CURRENT
  31.        
  32.     int mLightOn=  1000;
  33.         int mLightOff= 1000;
  34.         int mColor;
  35.         int mId;
  36.         int mIconIdentifier = android.R.drawable.ic_dialog_info;
  37.        
  38.         String mTitle = "LAMW";        
  39.         String mSubject = "Hello";
  40.         String mBody = "LAMW: Hello World!";
  41.         boolean mAutoCancel = true;
  42.         boolean mOngoing = false;
  43.        
  44.         private int mPriority = Notification.PRIORITY_LOW; //Notification.PRIORITY_DEFAULT;   //Notification.PRIORITY_LOW; //Notification.PRIORITY_HIGH;
  45.         private int mVisibility = Notification.VISIBILITY_PRIVATE;
  46.  
  47.     int NOTIFICATION_ID = 234;
  48.  
  49.     String cannal_id = "my_lamw_channel_01";
  50.     CharSequence name = "my_lamw_channel";
  51.     //String Description = "This is my lamw channel";
  52.     int importance = NotificationManager.IMPORTANCE_LOW;
  53.  
  54.     //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  55.     public jNotificationManager(Controls _ctrls, long _Self) { //Add more others news "_xxx" params if needed!
  56.        //super(_ctrls.activity);
  57.        context   = _ctrls.activity;
  58.        pascalObj = _Self;
  59.        controls  = _ctrls;
  60.  
  61.         Random random = new Random();
  62.         int randomInteger = random.nextInt(11000);
  63.         cannal_id =  "cannal_" + String.valueOf(randomInteger);
  64.  
  65.         String packageName = this.controls.activity.getPackageName();
  66.         name = cannal_id + "_" + packageName;
  67.  
  68.         mNotificationManager = (NotificationManager)controls.activity.getSystemService(Context.NOTIFICATION_SERVICE);
  69.  
  70.         //https://stackoverflow.com/questions/45711925/failed-to-post-notification-on-channel-null-target-api-is-26
  71.         //[ifdef_api26up]
  72.         if(Build.VERSION.SDK_INT >= 26) {
  73.             NotificationChannel mChannel = new NotificationChannel(cannal_id, name, importance);
  74.             mChannel.enableLights(true);
  75.             mNotificationManager.createNotificationChannel(mChannel);
  76.             mNotificationBuilder = new Notification.Builder(controls.activity, cannal_id);
  77.         }//[endif_api26up]
  78.  
  79.         if(Build.VERSION.SDK_INT < 26)
  80.            mNotificationBuilder = new Notification.Builder(controls.activity);  //need API >= 11 !!
  81.  
  82.     }
  83.  
  84.     public void jFree() {
  85.       //free local objects...
  86.         mNotificationManager = null;
  87.         mNotificationBuilder = null;
  88.         mNotification =  null;
  89.     }
  90.  
  91.   //write others [public] methods code here......
  92.   //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  93.  
  94.   //by jmpessoa
  95.     public int GetDrawableResourceId(String _resName) {
  96.           try {
  97.              Class<?> res = R.drawable.class;
  98.              Field field = res.getField(_resName);  //"drawableName"
  99.              int drawableId = field.getInt(null);
  100.              return drawableId;
  101.           }
  102.           catch (Exception e) {
  103.              Log.e("jNotificationManager", "Failure to get drawable id.", e);
  104.              return 0;
  105.           }
  106.     }
  107.        
  108.     //thanks to freris    
  109.     public void SetLightsColorAndTime(int _color, int _onMills, int _offMills) {
  110.         if (mNotificationBuilder != null) {
  111.            mColor = _color;                
  112.                    
  113.            if ( _onMills  > 0 ) mLightOn=  _onMills;                           
  114.            if ( _offMills > 0)  mLightOff= _offMills;
  115.            
  116.            mNotificationBuilder.setLights(_color, mLightOn, mLightOff);            
  117.            //Solution by freris            
  118.            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  119.               mNotificationManager.notify(mId, mNotificationBuilder.build());
  120.            }
  121.            else {
  122.               mNotificationManager.notify(mId, mNotificationBuilder.getNotification());          
  123.            }               
  124.            
  125.         }
  126.     }
  127.    
  128.     //thanks to freris
  129.     public void SetLightsEnable(boolean _enable) {
  130.         if (mNotificationBuilder != null) {
  131.                
  132.             if (!_enable)      
  133.               mNotificationBuilder.setLights(mColor, 0, 0);        
  134.             else
  135.                   mNotificationBuilder.setLights(mColor, mLightOn, mLightOff);
  136.            
  137.              if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN ) {
  138.                      mNotification =   mNotificationBuilder.getNotification();             
  139.                 //mNotificationManager.notify(mId, mNotification);                 
  140.              }
  141.  
  142.              if ( (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && (Build.VERSION.SDK_INT < 21) ) {
  143.                  //[ifdef_api16up]
  144.                  if(Build.VERSION.SDK_INT >= 16)
  145.                      mNotification =  mNotificationBuilder.build();
  146.                  //[endif_api16up]
  147.              }  //https://www.laurivan.com/android-make-your-notification-sticky/
  148.                
  149.            //[ifdef_api21up]
  150.              if (Build.VERSION.SDK_INT >= 21) {  //Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
  151.                  mNotification =  mNotificationBuilder.build();
  152.                  if (mPriority == Notification.PRIORITY_HIGH) { //to make Heads-up work.                 
  153.                         //mNotificationBuilder.setDefaults(Notification.DEFAULT_ALL); // must requires VIBRATE permission                
  154.                     mNotificationBuilder.setVibrate(new long[0]);
  155.                     //mNotificationBuilder.setVibrate(new long[] {1, 1, 1});
  156.                  }                     
  157.              }   
  158.              //[endif_api21up]
  159.              
  160.              mNotificationManager.notify(mId, mNotification);              
  161.            
  162.         }
  163.     }    
  164.    
  165.     //This method cancel a previously shown notification.
  166.     public void Cancel(int _id) {
  167.       mNotificationManager.cancel(_id);    
  168.     }
  169.    
  170.     public void CancelAll() {
  171.       mNotificationManager.cancelAll();    
  172.     }  
  173.  
  174.     //TODO Pascal
  175.     public void SetDeleteIntent(String _packageName, String _activityClassName) {       //the activity to be triggered
  176.         Intent intent = new Intent();
  177.         Class<?> cls = null;        
  178.             try {
  179.                         cls = Class.forName(_packageName+'.'+_activityClassName);
  180.                 } catch (ClassNotFoundException e) {
  181.                         // TODO Auto-generated catch block
  182.                         e.printStackTrace();
  183.                 }
  184.             if (cls != null)
  185.                 intent.setClass(controls.activity, cls);                                   
  186.         PendingIntent pendingIntent = PendingIntent.getActivity(context, mId, intent, PendingIntent.FLAG_CANCEL_CURRENT);        
  187.         mNotificationBuilder.setDeleteIntent(pendingIntent);           
  188.    }
  189.    
  190.     //https://www.laurivan.com/android-display-a-notification/
  191.    
  192.     public void SetContentIntent(Intent _intent) {                           
  193.           _intent.putExtra("content", mTitle + ";" + mSubject+ ";" + mBody);
  194.           PendingIntent pendingIntent = PendingIntent.getActivity(context, mId, _intent, mPendingFlag);                  
  195.           mNotificationBuilder.setContentIntent(pendingIntent);          
  196.     }
  197.        
  198.     //http://www.coderzheaven.com/2016/02/20/adding-notifications-reading-notifications-getting-number-of-notifications-in-android-m/
  199.     public void SetContentIntent(Intent _intent, int _broadcastRequestCode) {   //.FLAG_UPDATE_CURRENT
  200.         _intent.putExtra("content", mTitle + ";" + mSubject+ ";" + mBody);
  201.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _broadcastRequestCode, _intent, mPendingFlag);       
  202.             mNotificationBuilder.setContentIntent(pendingIntent);          
  203.     }
  204.    
  205.     //https://www.laurivan.com/android-notifications-with-custom-layout/
  206.     //mNotificationBuilder.setOnlyAlertOnce
  207.     //mNotificationBuilder.setSound (Uri sound)    
  208.     //mNotificationBuilder.setStyle (Notification.Style style)   // API level 16
  209.        
  210.     public void SetContentIntent(String _packageName, String _activityClassName) {      //the activity to be triggered
  211.         Intent intent = new Intent();
  212.         Class<?> cls = null;        
  213.             try {
  214.                         cls = Class.forName(_packageName+'.'+_activityClassName);
  215.                 } catch (ClassNotFoundException e) {
  216.                         // TODO Auto-generated catch block
  217.                         e.printStackTrace();
  218.                 }
  219.             if (cls != null)
  220.                 intent.setClass(controls.activity, cls);
  221.            
  222.                 intent.putExtra("content", mTitle + ";" + mSubject+ ";" + mBody);
  223.                
  224.         PendingIntent pendingIntent = PendingIntent.getActivity(context, mId, intent, mPendingFlag);     
  225.         mNotificationBuilder.setContentIntent(pendingIntent);
  226.        
  227.     }
  228.        
  229.     public void SetContentIntent(String _packageName, String _activityClassName, String dataName, String dataValue) {           //the activity to be triggered
  230.         Intent intent = new Intent();
  231.         Class<?> cls = null;        
  232.             try {
  233.                         cls = Class.forName(_packageName+'.'+_activityClassName);
  234.                 } catch (ClassNotFoundException e) {
  235.                         // TODO Auto-generated catch block
  236.                         e.printStackTrace();
  237.                 }
  238.             if (cls != null)
  239.                 intent.setClass(controls.activity, cls);                                 
  240.            
  241.             intent.putExtra("content", mTitle + ";" + mSubject+ ";" + mBody);      
  242.             intent.putExtra(dataName, dataValue);          
  243.            
  244.         PendingIntent pendingIntent = PendingIntent.getActivity(context, mId, intent, mPendingFlag);     
  245.         mNotificationBuilder.setContentIntent(pendingIntent);          
  246.    }    
  247.            
  248.    public void SetIconIdentifier(String _iconIdentifier) {
  249.     //int icon =android.R.drawable.ic_dialog_info;     
  250.            mIconIdentifier = GetDrawableResourceId(_iconIdentifier) ;
  251.    }
  252.        
  253.    public void SetTitle(String _title) {
  254.      mTitle = _title;
  255.    }
  256.    
  257.    public void SetSubject(String _subject) {
  258.            mSubject = _subject;
  259.    }
  260.    
  261.    public void SetBody(String  _body) {
  262.            mBody = _body;
  263.    }
  264.      
  265.    public void SetId(int _id) {
  266.                 mId = _id;
  267.    }
  268.    
  269.    public void SetAutoCancel(boolean _value) {
  270.            mAutoCancel = _value;
  271.    }
  272.    
  273.    //n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;    
  274.    public void SetOngoing(boolean _value) {
  275.        mOngoing = _value;
  276.    }
  277.    
  278.    public void Notify() {
  279.                
  280.      mNotificationBuilder.setContentTitle(mTitle);        
  281.      mNotificationBuilder.setContentText(mSubject);
  282.      mNotificationBuilder.setContentInfo(mBody);      
  283.      mNotificationBuilder.setSmallIcon(mIconIdentifier);  
  284.      mNotificationBuilder.setLights(mColor, mLightOn, mLightOff); //thanks to freris
  285.      mNotificationBuilder.setAutoCancel(mAutoCancel);
  286.      mNotificationBuilder.setOngoing(mOngoing);
  287.  
  288.        //[ifdef_api26up]
  289.        if(Build.VERSION.SDK_INT >= 26)
  290.            mNotificationBuilder.setChannelId(cannal_id);
  291.        //[endif_api26up]
  292.  
  293.            //[ifdef_api16up]
  294.        if(Build.VERSION.SDK_INT >= 16)
  295.           mNotificationBuilder.setPriority(mPriority);//https://stackoverflow.com/questions/26451893/heads-up-notification-android-lollipop
  296.        //[endif_api16up]
  297.  
  298.      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN ) {
  299.            mNotification =   mNotificationBuilder.getNotification();               
  300.        //mNotificationManager.notify(mId, mNotification);                  
  301.      }
  302.  
  303.      if ( (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && (Build.VERSION.SDK_INT < 21) ) {
  304.          //[ifdef_api16up]
  305.          if(Build.VERSION.SDK_INT >= 16)
  306.             mNotification =  mNotificationBuilder.build();
  307.          //[endif_api16up]
  308.      }  //https://www.laurivan.com/android-make-your-notification-sticky/
  309.      
  310.    //[ifdef_api21up]
  311.      if (Build.VERSION.SDK_INT >= 21) {  //Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
  312.          mNotification =  mNotificationBuilder.build();
  313.          if (mPriority == Notification.PRIORITY_HIGH) { //to make Heads-up work.                 
  314.                 //mNotificationBuilder.setDefaults(Notification.DEFAULT_ALL); // must requires VIBRATE permission                
  315.             mNotificationBuilder.setVibrate(new long[0]);
  316.             //mNotificationBuilder.setVibrate(new long[] {1, 1, 1});
  317.          }              
  318.      }   
  319.    //[endif_api21up]
  320.      
  321.      mNotificationManager.notify(mId, mNotification);
  322.    }
  323.    
  324.    public void SetPendingFlag(int _flag) {
  325.            mPendingFlag = _flag;
  326.    }
  327.                
  328.    public void SetPriority(int _priority) {
  329.            mPriority = _priority;
  330.            //Notification.PRIORITY_DEFAULT;   //Notification.PRIORITY_LOW; //Notification.PRIORITY_HIGH;
  331.    }
  332.    
  333.    public void SetVisibility(int _visibility) {
  334.            mVisibility = _visibility;  //VISIBILITY_SECRET, VISIBILITY_PRIVATE, VISIBILITY_PUBLIC
  335.    }
  336. }
  337.  

but i try change the importance and the visibility in runtime and not work because the importance is set when the notificationmanager is create, you can improved it to change the importance and visiblity functions?.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018