Why is my notification not showing when I create an asynchronous background service from a callback?

I created a service, not an intenservice, as I need to do some asynchronous things (location + server call) and finally create a notification.

First, I had a transmitter that created a notification and nothing else. This works great. However, in the service, I start the location manager and then in the callback, it starts the asynchronous call, and in the THAT callback, try to create a notification, it doesn't show up in the inbox!

It drives me crazy. I tried toast as you can see in the code and it works ... I am very grateful for the help.

Code in my service (simplified a bit for brevity). Where everything works fine, the server is called, methods are registered, etc., but the box shows NO Notification (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!
    }
}

      

+3


source to share


1 answer


try it as manager.notify ( new Random (). nextInt () , notification); you have to pass different credentials every time, otherwise it will be replaced with the old one otherwise



0


source







All Articles