1. Are you ready for the Galaxy S20? Here is everything we know so far!

Can't Update Notifications text after Forerground service init; Context issue

Discussion in 'Android Development' started by iamjkdn, Aug 19, 2018.

  1. iamjkdn

    iamjkdn Lurker
    Thread Starter

    Hello
    I have a simple NanoHTTPD server running in Android. It serves music files from the storage.

    I have created a Foreground service to make it running. Now this part works. It also shows the notification in the panel.

    But I am having trouble updating the notification with new text. Basically I just want to display the new icoming file request URL in the notification. How can I do that?

    I tried using Notification.notify() but the I cant understand the context to pass in the non-activity java file which defines the NanoHTTPD server.

    File structure
    - Mainactivity
    - NanoServer (server implementaion)
    - NanoService (foreground service class)
    - NotificationProvider (separate class to handle notifications)

    NanoServer.java
    Code (Java):
    1. public class NanoServer extends NanoHTTPD {
    2.     public Context context = getContext();
    3.     public NotificationProvider notificationProvider;
    4.  
    5.     public NanoServer(int port) {
    6.         super(8089);
    7.     }
    8.  
    9.     @Override
    10.     public Response serve(String uri, Method method,
    11.                           Map<String, String> header, Map<String, String> parameters,
    12.                           Map<String, String> files) {
    13.         String answer = "";
    14.         String msg;
    15.  
    16.         // doesnt work with the context. something wrong here I guess????
    17.         notificationProvider = new NotificationProvider();
    18.         notificationProvider.setNotification(context, "Title", uri, 0);
    19.  
    20.         FileInputStream fis = null;
    21.  
    22.         try {
    23.             fis = new FileInputStream(uri);
    24.             Log.w(TAG, uri + " found");
    25.         } catch (FileNotFoundException e) {
    26.             // TODO Auto-generated catch block
    27.             e.printStackTrace();
    28.         }
    29.         return newChunkedResponse(Status.OK, "audio/mpeg", fis);
    30.  
    31.     }
    32.  
    33.     public Context getContext() {
    34.         return context;
    35.     }
    36. }
    NanoService.java
    Code (Java):
    1.     String TAG = "NANOSERVICE";
    2.     public Context context = this;
    3.     public Handler handler = null;
    4.     public static Runnable runnable = null;
    5.     PowerManager powerManager;
    6.     PowerManager.WakeLock wakeLock;
    7.     WifiManager.WifiLock wifiLock;
    8.  
    9.     private NanoServer nanoServer;
    10.     public NotificationProvider notificationProvider;
    11.  
    12.     public NanoService() {
    13.     }
    14.  
    15.     @Override
    16.     public IBinder onBind(Intent intent) {
    17.         // TODO: Return the communication channel to the service.
    18.         throw new UnsupportedOperationException("Not yet implemented");
    19.     }
    20.  
    21.     @Override
    22.     public int onStartCommand(Intent intent, int flags, int startId) {
    23.         // Start the httpd.
    24.         try {
    25.             nanoServer = new NanoServer(8089);
    26.             nanoServer.start();
    27.             Log.d(TAG, "Service with server started");
    28.         } catch (IOException e) {
    29.             e.printStackTrace();
    30.             Toast.makeText(this, "Service failed to start.", Toast.LENGTH_LONG).show();
    31.         }
    32.  
    33.         // Keep the CPU awake (but not the screen).
    34.         powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
    35.         wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    36.         wakeLock.acquire();
    37.  
    38.         // Keep the WIFI turned on.
    39.         WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    40.         wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG);
    41.         wifiLock.acquire();
    42.  
    43.         notificationProvider = new NotificationProvider();
    44.         notificationProvider.setNotification(this, "Title", "Message", 0);
    45.  
    46.         // had to extend notificationprovider with notification
    47.         startForeground(1, notificationProvider);
    48.         Log.d(TAG, "Foreground service running");
    49.  
    50.         return Service.START_STICKY;
    51.  
    52.     }
    53.  
    54.     @Override
    55.     public void onDestroy() {
    56.         stopForeground(true);
    57.         wakeLock.release();
    58.         wifiLock.release();
    59.         nanoServer.stop();
    60.     }
    NotificationProvider.java
    Code (Java):
    1. public class NotificationProvider extends Notification {
    2.     String TAG = "NOTIFICATIONPROVIDER";
    3.     public NotificationProvider() {
    4.     }
    5.  
    6.     public void setNotification(Context context, String notificationTitle, String notificationMessage, int notificationRequestCode){
    7.         NotificationCompat.Builder builder =
    8.                 new NotificationCompat.Builder(context)
    9.                         .setSmallIcon(R.drawable.ic_launcher_background)
    10.                         .setContentTitle(notificationTitle)
    11.                         .setContentText(notificationMessage)
    12.                         .setTicker("My service")
    13.                         .setColor(101)
    14.                         .setWhen(System.currentTimeMillis())
    15.                         .setOngoing(true)
    16.                         .setPriority(NotificationCompat.PRIORITY_MAX);
    17.  
    18.         Intent intent = new Intent(context, MainActivity.class);
    19.         PendingIntent contentIntent = PendingIntent.getActivity(context, notificationRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    20.         builder.setContentIntent(contentIntent);
    21.  
    22.         NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    23.         manager.notify(0, builder.build());
    24.         Log.d(TAG, "Got new Notification");
    25.     }
    26. }
     


Loading...

Share This Page

Loading...