I have created a Service, not an intentservice since i need to do some asynchronous stuff (location + server call), and finally create a Notification.
I first had a broadcastreceiver that created the notification, nothing more. That works fine. However, in the Service i start a a locationmanager, then in the callback start an async, and in THAT callback try to create a Notification, it is not shown in the drawer!
It's driving me crazy. I tried a toast as you can see in the code, and that works... I would really appreciate help.
Code in my service (simplifed a bit for brevity) Where everything works fine, server is called, methods are logged etc, but NO Notification appears in drawer (only if i call cleanup directly from onStartCommand):
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
//.... doing various other stuff
handleAsyncStuff(session);
return START_STICKY;
}
....in my callback from final async server rest call:
private void cleanup(String header, String message){
try{
Log.d(TAG, "cleanup;header;" + header + ";message;" + message);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), message, duration);
toast.show();
Intent notificationIntent = new Intent(getApplicationContext(), MainTabActivity.class);
notificationIntent.putExtra(PreferencesHandler.INTENT_HEADER_PARAM, header); notificationIntent.putExtra(PreferencesHandler.INTENT_MESSAGE_PARAM, message);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle(header)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
PendingIntent pendingIntent = PendingIntent.getActivity(GeofenceBackgroundService.this.getApplicationContext(),
NOTIFICATION_ID,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
}finally{
//stopSelf(); Comment out did not make notification appear!
}
}