Reputation: 446
My app creates local notifications (using a notification manager via a dependency service) for certain things whilst it's running. However, if the user fully closes* the app, i.e. swipes it away in the overview view or whatever you call it, any notifications that still exist hang around afterward.
If the user then swipes a notification away, I get an 'app has stopped' dialog.
This is a really difficult problem to search for on the internet as no combination of search terms brings up anything other than people wanting to push notifications when the app is closed. I can't believe I'm the first person to have this problem but there is nothing at all that I can find that mentions it.
Ideally, I would simply cancel all the notifications when the app is closed, but this seems to be impossible. I've tried overriding OnDestroy() and OnStop() in MainActivity to cancel them all but it doesn't do anything (which raises the question that if this is because the app is dead at this point so no code can run then what's the point of these events?).
So, the question is: How can I cancel all the notifications when the app is closed or, failing that, how can I stop this crash when the user swipes away a notification when the app isn't running?
*I've never been able to find a definitive term for this that disambiguates it from 'backgrounding'/'sleeping' or 'killing'/'force killing'.
Upvotes: 1
Views: 126
Reputation: 113
write notification code in reciver class :
public class MyReciver : Android.Content.BroadcastReceiver{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == "pushnotification"){
this.SendNotification();
}
public void Sendnotification(){
//notification code
}
}
}
then call reciver from service class : OnTaskRemoved method restart notification code when app is closed
class MyServices : IntentService
{
protected override void OnHandleIntent(Intent intent)
{
}
public override void OnTaskRemoved(Intent rootIntent)
{
MyReciver receiver_notify = new MyReciver();
IntentFilter intent_notify = new IntentFilter("pushnotification");
RegisterReceiver(receiver_notify, intent_notify);
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
MyReciver receiver_notify = new MyReciver();
IntentFilter intent_notify = new IntentFilter("pushnotification");
RegisterReceiver(receiver_notify, intent_notify);;
}
}
then call MyServices class where you need run notification
Upvotes: 1