I have a simple NanoHTTPD server running as a foreground service.
I am facing issues in updating the notifications with new content when a new request to a server comes in.
The foreground service starts up and the notification shows. No issue there. But can't update them later.
File structure
- Mainactivity
- NanoServer (server implementaion)
- NanoService (foreground service class)
- NotificationProvider (separate class to handle notifications)
NanoServer.java
public Context context = getContext();
public NotificationProvider notificationProvider;
public NanoServer(int port) {
super(8089);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
String msg;
// doesnt work with the context. something wrong here I guess????
notificationProvider = new NotificationProvider();
notificationProvider.setNotification(context, "Title", uri, 0);
FileInputStream fis = null;
try {
fis = new FileInputStream(uri);
Log.w(TAG, uri + " found");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
}
public Context getContext() {
return context;
}
NanoService.java
String TAG = "NANOSERVICE";
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
WifiManager.WifiLock wifiLock;
private NanoServer nanoServer;
public NotificationProvider notificationProvider;
public NanoService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Start the httpd.
try {
nanoServer = new NanoServer(8089);
nanoServer.start();
Log.d(TAG, "Service with server started");
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Service failed to start.", Toast.LENGTH_LONG).show();
}
// Keep the CPU awake (but not the screen).
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire();
// Keep the WIFI turned on.
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG);
wifiLock.acquire();
notificationProvider = new NotificationProvider();
notificationProvider.setNotification(this, "Title", "Message", 0);
// had to extend notificationprovider with notification
startForeground(1, notificationProvider);
Log.d(TAG, "Foreground service running");
return Service.START_STICKY;
}
@Override
public void onDestroy() {
stopForeground(true);
wakeLock.release();
wifiLock.release();
nanoServer.stop();
}
NotificationProvider.java
public class NotificationProvider extends Notification {
String TAG = "NOTIFICATIONPROVIDER";
public NotificationProvider() {
}
public void setNotification(Context context, String notificationTitle, String notificationMessage, int notificationRequestCode){
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage)
.setTicker("My service")
.setColor(101)
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, notificationRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
Log.d(TAG, "Got new Notification");
}
}
context
insetNotification
. That created problems in the non-activity class NanoServer. So will this still work? Is the way I have defined the context in NanoServer correct? – Bolick