0

I have app, that send notifications to user time to time. Some of them are extremly important, so I've set them as ongoing, so user can't delete them by swiping, they are deleted when user deletes them in app (or when the app is force stopped).

When I create this notification, I add object with some data (name, text, id and datetime of creation of the notification) to static List, it works. But when there is some important ongoing notification and user closes the application (goes to the list of active app via the left hw button and swipe-closes it), the static List of notifications is cleared.

So how can I:

  • Save data (list of objects) until app is force-killed (so until the notification disappears)

OR

  • Get collection of active notifications created by application (need name and id, and if possible even text)

Thanks

EDIT: If you know something about developing mobile apps in Java, you can help me by saying how would you do that in java, Xamarin is pretty similar to it, so if you do that, I think, I'd be able to find a way.

SoptikHa
  • 437
  • 4
  • 19

1 Answers1

1

If you want to create a not-clear-able notification, for Api level below 11, set Notification.FLAG_NO_CLEAR.

For Api level above 11, set setOngoing to true, this will make notifications not be dismissed by user.

For example:

Notification.Builder builder = new Notification.Builder(this)
        .SetContentTitle("Sample Notification")
        .SetContentText("Hello World! This is my first notification!")
        .SetSmallIcon(Resource.Drawable.ic_notification)
        .SetOngoing(true);

After setting it to true, make sure your app/service will handle the canceling of this notification.

Update:

Get collection of active notifications created by application (need name and id, and if possible even text)

NotificationManager notifyManager = (NotificationManager)GetSystemService(Context.NotificationService);
StatusBarNotification[] notificationList = notifyManager.GetActiveNotifications();

foreach (Android.Service.Notification.StatusBarNotification sbnin notificationList)
{
    Debug.WriteLine("sbn.Id " + sbn.Id);
    Debug.WriteLine("sbn.PostTime " + sbn.PostTime);
    Debug.WriteLine("System.Diagnostics.Debug.WriteLine("notification = " + sbn.ToString());= " + sbn.ToString());
}

Save data (list of objects) until app is force-killed (so until the notification disappears)

As Samuh said, when you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens :

  • the class is unloaded
  • the JVM shuts down
  • the process dies

So your static List will exists until your app is force-killed. As you said,

user closes the application (goes to the list of active app via the left hw button and swipe-closes it), the static List of notifications is cleared.

So you have implement this feature, if this is not the effect your want, please elaborate a bit more.

If you want to save the list even when your app is closed, you can try to save your list in onstop and so on to a shared-preference or file.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Yes, I've already done this. But how can I either: 1) Get all active notifications created by the app 2) Save data after app is closed but until app is force-stopped I want to option user to only close the notification in-app, I'm doing it this way: When user creates notifications, I add it to static List. But when the app is closed (by swiping it, not by force-stopping it), the static list is cleared, but the notification remains there. How can I save data until game is force-stopped or how can I get list of my active notifications? Or how can I test if notification ID exists? – SoptikHa Sep 07 '17 at 13:13
  • 1
    @SoptikHa, sorry for the late responding, I updated my answer, you may have a look. – Grace Feng Sep 11 '17 at 02:46
  • Thanks! I'm not at home and don't have any computer here to test it, so I'll try it in 3 or 4 days. Thanks for your answer :-) – SoptikHa Sep 12 '17 at 06:31