1

I have a service1 broadcasts the service status to MainActivity, MainActivity sends a notification after receive the broadcast. This notification is flagged as a non cancelable notification. In order to cancel the notification when the app is killed from the task panel, I binded the activity with KillNotificationsService which is provided in Cancel notification on remove application from multitask panel for the solution to cancel notification. I also need to cancel the notification when service1 stops. So I put

   @Override
public void onDestroy(Intent intent) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(KillNotificationsService.NOTIFICATION_ID);
}

(NOTIFICATION ID is a static variable in KillNotificationsService).

After I canceled this notification, the next time I start service1, after service1 broadcasts the status, the notification with same id(NOTIFICATION_ID) won't appear. I tried to assign unique Ids and gc for the notification, but it didn't seem working.

If anyone can help me.

Community
  • 1
  • 1
Xin Zhang
  • 103
  • 1
  • 9
  • You need to explain exactly what you are trying to do and what is not working –  Apr 12 '16 at 12:25
  • I have edited the post, English is not my first language, let me know if it is clear. – Xin Zhang Apr 12 '16 at 12:32
  • Share the broadcast receiver used to display notification. Also the the broadcast sent from service. The code you shared seems ok. – drulabs Apr 12 '16 at 12:36
  • Yes, sharing the broadcast receiver is not only used to display notification, there are more UI works. Before that, I put notification inside server1, the notification will be canceled itself when I stop the service. However if I kill the app without stopping service, the notification will always be there. So I found the solution of KillNotificationsService. It is not possible to bind one service to another, so I pulled the notification out of the service. – Xin Zhang Apr 12 '16 at 13:09

1 Answers1

0

May be you are using context of broadcast receiver to build notification

use application context to build notification.

Context context=getApplicationContext();
NotificationManager mNotificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mNotificationManager.notify(KillNotificationsService.NOTIFICATION_ID, mBuilder.build());