3

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!
    }
}
Mathias
  • 3,879
  • 5
  • 36
  • 48
  • Are you certain that `cleanup()` is being called and that no exception is thrown in the method? You should add debug logging to be sure that the `manager.notify()` method is actually being called. – David Wasser May 02 '17 at 13:20
  • Yes, doing all that, thanks. Cleanup is indeed called, and i have a log after the manager call saying "manager called" :) – Mathias May 02 '17 at 13:30

1 Answers1

0

try it like manager.notify(new Random().nextInt(), notification); you have to pass different Notification Id each time otherwise it will replaced by old one

Rajesh N
  • 6,198
  • 2
  • 47
  • 58
  • Yes, but that is what i want. My problem is that *no notification at all* is displayed. And again, this is not a problem when i'm not creating the notification from the async callback. In all other places, my method calls results in a created notification. – Mathias May 02 '17 at 10:18