Skip to content

Instantly share code, notes, and snippets.

@c0mm4nDer
Created October 8, 2017 16:19
Show Gist options
  • Save c0mm4nDer/5b6885392e8c3281fbe22cfb40796a83 to your computer and use it in GitHub Desktop.
Save c0mm4nDer/5b6885392e8c3281fbe22cfb40796a83 to your computer and use it in GitHub Desktop.
One Signal ClearOneSignalNotifications For Xamarin.Android
/* All xamarin programmers know that binding libraries is not always done correctly.
One of these libraries is OneSignal. In OneSignal plugin, there is a method called "ClearOneSignalNotifications"
that clears all notifications displayed.
This method is not included in the binding provided for xamarin.For Fix this we should rewrite this method.
You can see original code of this method in [OneSignal SDK Android](https://github.com/OneSignal/OneSignal-Android-SDK/blob/b1c01e0404b3f39d433f48abfa79c99e2af1f6f0/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java).
I used ADO for connect to sqlite but you can rewrite it by any kind of sqlite connection. I hope this code can help you.*/
using Android.App;
using Android.Content;
using Mono.Data.Sqlite;
using System;
namespace OneSignalNotificationHelper
{
public class Notification
{
static class NotificationTable
{
public static readonly String _ID = "_id";
public static readonly String TABLE_NAME = "notification";
public static readonly String COLUMN_NAME_NOTIFICATION_ID = "notification_id"; // OneSignal Notification Id
public static readonly String COLUMN_NAME_ANDROID_NOTIFICATION_ID = "android_notification_id";
public static readonly String COLUMN_NAME_GROUP_ID = "group_id";
public static readonly String COLUMN_NAME_COLLAPSE_ID = "collapse_id";
public static readonly String COLUMN_NAME_IS_SUMMARY = "is_summary";
public static readonly String COLUMN_NAME_OPENED = "opened";
public static readonly String COLUMN_NAME_DISMISSED = "dismissed";
public static readonly String COLUMN_NAME_TITLE = "title";
public static readonly String COLUMN_NAME_MESSAGE = "message";
public static readonly String COLUMN_NAME_CREATED_TIME = "created_time";
}
public class OneSignalHelper
{
private static readonly string DATABASE_NAME = "OneSignal.db";
/// <summary>
/// Removes all OneSignal notifications from the Notification Shade. If you just use
/// {@link NotificationManager#cancelAll()}, OneSignal notifications will be restored when
/// your app is restarted.
/// </summary>
/// <param name="context"></param>
public static void ClearOneSignalNotifications(Context context)
{
try
{
// Clear all notification
NotificationManager nMgr = (NotificationManager)context.GetSystemService(Context.NotificationService);
string dbFilePath = Android.App.Application.Context.GetDatabasePath(DATABASE_NAME).AbsolutePath;
string databaseFolder = System.IO.Path.GetDirectoryName(dbFilePath);
var connection = new SqliteConnection("Data Source=" + dbFilePath);
connection.Open();
using (var contents = connection.CreateCommand())
{
contents.CommandText = string.Format("SELECT {0} FROM {1} WHERE {2} ", NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID, NotificationTable.TABLE_NAME, NotificationTable.COLUMN_NAME_DISMISSED + " = 0 AND " +
NotificationTable.COLUMN_NAME_OPENED + " = 0");
var item = contents.ExecuteReader();
Console.WriteLine("Reading data");
while (item.Read())
{
int existingId = Convert.ToInt32((long)item[NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID]);
nMgr.Cancel(existingId);
}
}
using (var contents = connection.CreateCommand())
{
string whereStr = NotificationTable.COLUMN_NAME_OPENED + " = 0";
string valueStr = NotificationTable.COLUMN_NAME_DISMISSED + "= 1";
contents.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2}", NotificationTable.TABLE_NAME, valueStr, whereStr);
contents.ExecuteNonQuery();
}
connection.Close();
}
catch { }
}
}
}
}
@c0mm4nDer
Copy link
Author

c0mm4nDer commented Oct 10, 2017

Usage:

using Com.Onesignal.Shortcutbadger;
using static Notification;

.
.
.

OneSignalHelper.ClearOneSignalNotifications(this);
ShortcutBadger.RemoveCount(this);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment