internal static AndroidNotificationIntentData ParseNotificationIntentData(AndroidJavaObject notificationIntent) { var id = notificationIntent.Call <int>("getIntExtra", "id", -1); if (id == -1) { return(null); } var channelId = notificationIntent.Call <string>("getStringExtra", "channelID"); var notification = new AndroidNotification(); notification.Title = notificationIntent.Call <string>("getStringExtra", "textTitle"); notification.Text = notificationIntent.Call <string>("getStringExtra", "textContent"); notification.ShouldAutoCancel = notificationIntent.Call <bool>("getBooleanExtra", "autoCancel", false); notification.UsesStopwatch = notificationIntent.Call <bool>("getBooleanExtra", "usesChronometer", false); notification.FireTime = notificationIntent.Call <long>("getLongExtra", "fireTime", -1L).ToDatetime(); notification.RepeatInterval = notificationIntent.Call <long>("getLongExtra", "repeatInterval", -1L).ToTimeSpan(); notification.Style = notificationIntent.Call <int>("getIntExtra", "style", 0).ToNotificationStyle(); notification.Color = notificationIntent.Call <int>("getIntExtra", "color", 0).ToColor(); notification.Number = notificationIntent.Call <int>("getIntExtra", "number", -1); notification.IntentData = notificationIntent.Call <string>("getStringExtra", "data"); notification.Group = notificationIntent.Call <string>("getStringExtra", "group"); notification.GroupSummary = notificationIntent.Call <bool>("getBooleanExtra", "groupSummary", false); notification.SortKey = notificationIntent.Call <string>("getStringExtra", "sortKey"); notification.GroupAlertBehaviour = notificationIntent.Call <int>("getIntExtra", "groupAlertBehaviour", 0).ToGroupAlertBehaviours(); return(new AndroidNotificationIntentData(id, channelId, notification)); }
/// <summary> /// Schedule a notification which will be shown at the time specified in the notification struct. /// The specified id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus. /// </summary> public static void SendNotificationWithExplicitID(AndroidNotification notification, string channelId, int id) { if (Initialize()) { SendNotification(id, notification, channelId); } }
internal static AndroidNotificationIntentData ParseNotificationIntentData(AndroidJavaObject notificationIntent) { var id = notificationIntent.Call <int>("getIntExtra", "id", -1); var channel = notificationIntent.Call <string>("getStringExtra", "channelID"); if (id == -1) { return(null); } var notification = new AndroidNotification(); notification.title = notificationIntent.Call <string>("getStringExtra", "textTitle"); notification.text = notificationIntent.Call <string>("getStringExtra", "textContent"); notification.shouldAutoCancel = notificationIntent.Call <bool>("getBooleanExtra", "autoCancel", false); notification.usesStopwatch = notificationIntent.Call <bool>("getBooleanExtra", "usesChronometer", false); notification.fireTime = notificationIntent.Call <long>("getLongExtra", "fireTime", -1L); notification.repeatInterval = notificationIntent.Call <long>("getLongExtra", "repeatInterval", -1L); notification.style = notificationIntent.Call <int>("getIntExtra", "style", -1); notification.color = notificationIntent.Call <int>("getIntExtra", "color", 0); notification.number = notificationIntent.Call <int>("getIntExtra", "number", -1); notification.intentData = notificationIntent.Call <string>("getStringExtra", "data"); notification.group = notificationIntent.Call <string>("getStringExtra", "group"); notification.groupSummary = notificationIntent.Call <bool>("getBooleanExtra", "groupSummary", false); notification.sortKey = notificationIntent.Call <string>("getStringExtra", "sortKey"); notification.groupAlertBehaviour = notificationIntent.Call <int>("getIntExtra", "groupAlertBehaviour", -1); return(new AndroidNotificationIntentData { id = id, channel = channel, notification = notification, }); }
/// <summary> /// Schedule a notification which will be shown at the time specified in the notification struct. /// The specified id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus. /// </summary> public static void SendNotificationWithExplicitID(AndroidNotification notification, string channel, int id) { if (!Initialize()) { return; } SendNotification(id, notification, channel); }
internal static void SendNotification(int id, AndroidNotification notification, string channel) { if (notification.fireTime < 0L) { Debug.LogError("Failed to schedule notification, it did not contain a valid FireTime"); } AndroidJavaClass managerClass = new AndroidJavaClass("com.unity.androidnotifications.UnityNotificationManager"); AndroidJavaObject context = notificationManager.Get <AndroidJavaObject>("mContext"); AndroidJavaObject activity = notificationManager.Get <AndroidJavaObject>("mActivity"); AndroidJavaObject notificationIntent = new AndroidJavaObject("android.content.Intent", context, managerClass); AndroidJavaObject androidContext = notificationManager.Get <AndroidJavaObject>("mContext"); int smallIconId = notificationManager.CallStatic <int>("findResourceidInContextByName", notification.smallIcon, androidContext, activity); int largeIconId = notificationManager.CallStatic <int>("findResourceidInContextByName", notification.largeIcon, androidContext, activity); if (smallIconId == 0) { smallIconId = notificationManager.CallStatic <int>("findResourceidInContextByName", DEFAULT_APP_ICON_ADAPTIVE, androidContext, activity); if (smallIconId == 0) { smallIconId = notificationManager.CallStatic <int>("findResourceidInContextByName", DEFAULT_APP_ICON_LEGACY, androidContext, activity); } } notificationIntent.Call <AndroidJavaObject>("putExtra", "id", id); notificationIntent.Call <AndroidJavaObject>("putExtra", "channelID", channel); notificationIntent.Call <AndroidJavaObject>("putExtra", "textTitle", notification.title); notificationIntent.Call <AndroidJavaObject>("putExtra", "textContent", notification.text); notificationIntent.Call <AndroidJavaObject>("putExtra", "smallIcon", smallIconId); notificationIntent.Call <AndroidJavaObject>("putExtra", "autoCancel", notification.shouldAutoCancel); notificationIntent.Call <AndroidJavaObject>("putExtra", "usesChronometer", notification.usesStopwatch); notificationIntent.Call <AndroidJavaObject>("putExtra", "fireTime", notification.fireTime); notificationIntent.Call <AndroidJavaObject>("putExtra", "repeatInterval", notification.repeatInterval); notificationIntent.Call <AndroidJavaObject>("putExtra", "largeIcon", largeIconId); notificationIntent.Call <AndroidJavaObject>("putExtra", "style", notification.style); notificationIntent.Call <AndroidJavaObject>("putExtra", "color", notification.color); notificationIntent.Call <AndroidJavaObject>("putExtra", "number", notification.number); notificationIntent.Call <AndroidJavaObject>("putExtra", "data", notification.intentData); notificationIntent.Call <AndroidJavaObject>("putExtra", "group", notification.group); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupSummary", notification.groupSummary); notificationIntent.Call <AndroidJavaObject>("putExtra", "sortKey", notification.sortKey); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupAlertBehaviour", notification.groupAlertBehaviour); notificationManager.Call("scheduleNotificationIntent", notificationIntent); }
/// <summary> /// Schedule a notification which will be shown at the time specified in the notification struct. /// The specified id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus. /// </summary> /// <param name="notification">Data for the notification</param> /// <param name="channelId">ID of the channel to send notification to</param> /// <param name="id">A unique ID for the notification</param> public static void SendNotificationWithExplicitID(AndroidNotification notification, string channelId, int id) { if (Initialize()) { using (var builder = CreateNotificationBuilder(id, notification, channelId)) { SendNotification(builder); } } }
/// <summary> /// Create Notification.Builder. /// Will automatically generate the ID for notification. /// <see cref="CreateNotificationBuilder(int, AndroidNotification, string)"/> /// </summary> public static AndroidJavaObject CreateNotificationBuilder(AndroidNotification notification, string channelId) { AndroidJavaObject builder, extras; CreateNotificationBuilder(notification, channelId, out builder, out extras); if (extras != null) { extras.Dispose(); } return(builder); }
/// <summary> /// Update an already scheduled notification. /// If a notification with the specified id was already scheduled it will be overridden with the information from the passed notification struct. /// </summary> public static void UpdateScheduledNotification(int id, AndroidNotification notification, string channel) { if (!Initialize()) { return; } if (notificationManager.CallStatic <bool>("checkIfPendingNotificationIsRegistered", id)) { SendNotification(id, notification, channel); } }
/// <summary> /// Create Notification.Builder object on Java side using privided AndroidNotification. /// </summary> /// <param name="id">ID for the notification</param> /// <param name="notification">Struct with notification data</param> /// <param name="channelId">Channel id</param> /// <returns>A proxy object for created Notification.Builder</returns> public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotification notification, string channelId) { AndroidJavaObject builder, extras; CreateNotificationBuilder(notification, channelId, out builder, out extras); if (extras != null) { s_Jni.Bundle.PutInt(extras, s_Jni.NotificationManager.KEY_ID, id); extras.Dispose(); } return(builder); }
internal static AndroidNotificationIntentData GetNotificationData(AndroidJavaObject notificationObj) { using (var extras = s_Jni.Notification.Extras(notificationObj)) { var id = s_Jni.Bundle.GetInt(extras, s_Jni.NotificationManager.KEY_ID, -1); if (id == -1) { return(null); } var channelId = s_Jni.NotificationManager.GetNotificationChannelId(notificationObj); int flags = s_Jni.Notification.Flags(notificationObj); var notification = new AndroidNotification(); notification.Title = s_Jni.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TITLE); notification.Text = s_Jni.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TEXT); notification.SmallIcon = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_SMALL_ICON); notification.LargeIcon = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_LARGE_ICON); notification.ShouldAutoCancel = 0 != (flags & s_Jni.Notification.FLAG_AUTO_CANCEL); notification.UsesStopwatch = s_Jni.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_CHRONOMETER, false); notification.FireTime = s_Jni.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, -1L).ToDatetime(); notification.RepeatInterval = s_Jni.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, -1L).ToTimeSpan(); notification.ShowInForeground = s_Jni.Bundle.GetBoolean(extras, s_Jni.NotificationManager.KEY_SHOW_IN_FOREGROUND, true); if (s_Jni.Bundle.ContainsKey(extras, s_Jni.Notification.EXTRA_BIG_TEXT)) { notification.Style = NotificationStyle.BigTextStyle; } else { notification.Style = NotificationStyle.None; } notification.Color = s_Jni.NotificationManager.GetNotificationColor(notificationObj); notification.Number = s_Jni.Notification.Number(notificationObj); notification.IntentData = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA); notification.Group = s_Jni.Notification.GetGroup(notificationObj); notification.GroupSummary = 0 != (flags & s_Jni.Notification.FLAG_GROUP_SUMMARY); notification.SortKey = s_Jni.Notification.GetSortKey(notificationObj); notification.GroupAlertBehaviour = s_Jni.NotificationManager.GetNotificationGroupAlertBehavior(notificationObj).ToGroupAlertBehaviours(); var showTimestamp = s_Jni.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_WHEN, false); notification.ShowTimestamp = showTimestamp; if (showTimestamp) { notification.CustomTimestamp = s_Jni.Notification.When(notificationObj).ToDatetime(); } var data = new AndroidNotificationIntentData(id, channelId, notification); data.NativeNotification = notificationObj; return(data); } }
/// <summary> /// Schedule a notification which will be shown at the time specified in the notification struct. /// The returned id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus. /// </summary> public static int SendNotification(AndroidNotification notification, string channel) { if (!Initialize()) { return(-1); } int id = Math.Abs(DateTime.Now.ToString("yyMMddHHmmssffffff").GetHashCode()) + (new Random().Next(10000)); SendNotification(id, notification, channel); return(id); }
/// <summary> /// Schedule a notification which will be shown at the time specified in the notification struct. /// The returned id can later be used to update the notification before it's triggered, it's current status can be tracked using CheckScheduledNotificationStatus. /// </summary> /// <param name="notification">Data for the notification</param> /// <param name="channelId">ID of the channel to send notification to</param> /// <returns>The generated ID for the notification</returns> public static int SendNotification(AndroidNotification notification, string channelId) { if (!Initialize()) { return(-1); } int id; using (var builder = CreateNotificationBuilder(notification, channelId)) SendNotification(builder, out id); return(id); }
/// <summary> /// Update an already scheduled notification. /// If a notification with the specified id was already scheduled it will be overridden with the information from the passed notification struct. /// </summary> /// <param name="id">ID of the notification to update</param> /// <param name="notification">Data for the notification</param> /// <param name="channelId">ID of the channel to send notification to</param> public static void UpdateScheduledNotification(int id, AndroidNotification notification, string channelId) { if (!Initialize()) { return; } if (s_Jni.NotificationManager.CheckIfPendingNotificationIsRegistered(id)) { using (var builder = CreateNotificationBuilder(id, notification, channelId)) { SendNotification(builder); } } }
internal static void SendNotification(int id, AndroidNotification notification, string channel) { if (notification.fireTime < 0L) { Debug.LogError("Failed to schedule notification, it did not contain a valid FireTime"); } AndroidJavaClass managerClass = new AndroidJavaClass("com.unity.androidnotifications.UnityNotificationManager"); AndroidJavaObject context = notificationManager.Get <AndroidJavaObject>("mContext"); AndroidJavaObject activity = notificationManager.Get <AndroidJavaObject>("mActivity"); AndroidJavaObject notificationIntent = new AndroidJavaObject("android.content.Intent", context, managerClass); notificationIntent.Call <AndroidJavaObject>("putExtra", "id", id); notificationIntent.Call <AndroidJavaObject>("putExtra", "channelID", channel); notificationIntent.Call <AndroidJavaObject>("putExtra", "textTitle", notification.title); notificationIntent.Call <AndroidJavaObject>("putExtra", "textContent", notification.text); notificationIntent.Call <AndroidJavaObject>("putExtra", "smallIconStr", notification.smallIcon); notificationIntent.Call <AndroidJavaObject>("putExtra", "autoCancel", notification.shouldAutoCancel); notificationIntent.Call <AndroidJavaObject>("putExtra", "usesChronometer", notification.usesStopwatch); notificationIntent.Call <AndroidJavaObject>("putExtra", "fireTime", notification.fireTime); notificationIntent.Call <AndroidJavaObject>("putExtra", "repeatInterval", notification.repeatInterval); notificationIntent.Call <AndroidJavaObject>("putExtra", "largeIconStr", notification.largeIcon); notificationIntent.Call <AndroidJavaObject>("putExtra", "style", notification.style); notificationIntent.Call <AndroidJavaObject>("putExtra", "color", notification.color); notificationIntent.Call <AndroidJavaObject>("putExtra", "number", notification.number); notificationIntent.Call <AndroidJavaObject>("putExtra", "data", notification.intentData); notificationIntent.Call <AndroidJavaObject>("putExtra", "group", notification.group); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupSummary", notification.groupSummary); notificationIntent.Call <AndroidJavaObject>("putExtra", "sortKey", notification.sortKey); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupAlertBehaviour", notification.groupAlertBehaviour); notificationIntent.Call <AndroidJavaObject>("putExtra", "showTimestamp", notification.showTimestamp); long timestampValue = notification.showCustomTimestamp ? notification.customTimestamp : notification.fireTime; notificationIntent.Call <AndroidJavaObject>("putExtra", "showTimestamp", notification.showTimestamp); notificationIntent.Call <AndroidJavaObject>("putExtra", "timestamp", timestampValue); notificationManager.Call("scheduleNotificationIntent", notificationIntent); }
public void onSentNotification(AndroidJavaObject notificationIntent) { var notification = new AndroidNotification(); var id = notificationIntent.Call <int>("getIntExtra", "id", -1); var channel = notificationIntent.Call <string>("getStringExtra", "channelID"); notification.title = notificationIntent.Call <string>("getStringExtra", "textTitle"); notification.text = notificationIntent.Call <string>("getStringExtra", "textContent"); notification.shouldAutoCancel = notificationIntent.Call <bool>("getBooleanExtra", "autoCancel", false); notification.usesStopwatch = notificationIntent.Call <bool>("getBooleanExtra", "usesChronometer", false); notification.fireTime = notificationIntent.Call <long>("getLongExtra", "fireTime", -1L); notification.repeatInterval = notificationIntent.Call <long>("getLongExtra", "repeatInterval", -1L); notification.style = notificationIntent.Call <int>("getIntExtra", "style", -1); notification.color = notificationIntent.Call <int>("getIntExtra", "color", -1); notification.number = notificationIntent.Call <int>("getIntExtra", "number", -1); OnNotificationReceived(id, notification, channel); }
internal static void SendNotification(int id, AndroidNotification notification, string channelId) { long fireTime = notification.FireTime.ToLong(); if (fireTime < 0L) { Debug.LogError("Failed to schedule notification, it did not contain a valid FireTime"); } AndroidJavaClass managerClass = new AndroidJavaClass("com.unity.androidnotifications.UnityNotificationManager"); AndroidJavaObject context = s_NotificationManager.Get <AndroidJavaObject>("mContext"); AndroidJavaObject notificationIntent = new AndroidJavaObject("android.content.Intent", context, managerClass); notificationIntent.Call <AndroidJavaObject>("putExtra", "id", id); notificationIntent.Call <AndroidJavaObject>("putExtra", "channelID", channelId); notificationIntent.Call <AndroidJavaObject>("putExtra", "textTitle", notification.Title); notificationIntent.Call <AndroidJavaObject>("putExtra", "textContent", notification.Text); notificationIntent.Call <AndroidJavaObject>("putExtra", "smallIconStr", notification.SmallIcon); notificationIntent.Call <AndroidJavaObject>("putExtra", "autoCancel", notification.ShouldAutoCancel); notificationIntent.Call <AndroidJavaObject>("putExtra", "usesChronometer", notification.UsesStopwatch); notificationIntent.Call <AndroidJavaObject>("putExtra", "fireTime", fireTime); notificationIntent.Call <AndroidJavaObject>("putExtra", "repeatInterval", notification.RepeatInterval.ToLong()); notificationIntent.Call <AndroidJavaObject>("putExtra", "largeIconStr", notification.LargeIcon); notificationIntent.Call <AndroidJavaObject>("putExtra", "style", (int)notification.Style); notificationIntent.Call <AndroidJavaObject>("putExtra", "color", notification.Color.ToInt()); notificationIntent.Call <AndroidJavaObject>("putExtra", "number", notification.Number); notificationIntent.Call <AndroidJavaObject>("putExtra", "data", notification.IntentData); notificationIntent.Call <AndroidJavaObject>("putExtra", "group", notification.Group); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupSummary", notification.GroupSummary); notificationIntent.Call <AndroidJavaObject>("putExtra", "sortKey", notification.SortKey); notificationIntent.Call <AndroidJavaObject>("putExtra", "groupAlertBehaviour", (int)notification.GroupAlertBehaviour); notificationIntent.Call <AndroidJavaObject>("putExtra", "showTimestamp", notification.ShowTimestamp); long timestampValue = notification.ShowCustomTimestamp ? notification.CustomTimestamp.ToLong() : fireTime; notificationIntent.Call <AndroidJavaObject>("putExtra", "timestamp", timestampValue); s_NotificationManager.Call("scheduleNotificationIntent", notificationIntent); }
public AndroidNotificationIntentData(int id, string channel, AndroidNotification notification) { m_Id = id; m_Channel = channel; m_Notification = notification; }
/// <summary> /// Create an AndroidNotificationIntentData with AndroidNotification, id, and channel id. /// </summary> public AndroidNotificationIntentData(int id, string channelId, AndroidNotification notification) { Id = id; Channel = channelId; Notification = notification; }
static void CreateNotificationBuilder(AndroidNotification notification, string channelId, out AndroidJavaObject notificationBuilder, out AndroidJavaObject extras) { if (!Initialize()) { notificationBuilder = extras = null; return; } long fireTime = notification.FireTime.ToLong(); if (fireTime < 0L) { Debug.LogError("Failed to schedule notification, it did not contain a valid FireTime"); } // NOTE: JNI calls are expensive, so we avoid calls that set something that is also a default notificationBuilder = s_Jni.NotificationManager.CreateNotificationBuilder(channelId); s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_SMALL_ICON, notification.SmallIcon); if (!string.IsNullOrEmpty(notification.LargeIcon)) { s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_LARGE_ICON, notification.LargeIcon); } if (!string.IsNullOrEmpty(notification.Title)) { s_Jni.NotificationBuilder.SetContentTitle(notificationBuilder, notification.Title); } if (!string.IsNullOrEmpty(notification.Text)) { s_Jni.NotificationBuilder.SetContentText(notificationBuilder, notification.Text); } if (notification.ShouldAutoCancel) { s_Jni.NotificationBuilder.SetAutoCancel(notificationBuilder, notification.ShouldAutoCancel); } if (notification.Number >= 0) { s_Jni.NotificationBuilder.SetNumber(notificationBuilder, notification.Number); } if (notification.Style == NotificationStyle.BigTextStyle) { using (var style = new AndroidJavaObject("android.app.Notification$BigTextStyle")) { style.Call <AndroidJavaObject>("bigText", notification.Text).Dispose(); s_Jni.NotificationBuilder.SetStyle(notificationBuilder, style); } } long timestampValue = notification.ShowCustomTimestamp ? notification.CustomTimestamp.ToLong() : fireTime; s_Jni.NotificationBuilder.SetWhen(notificationBuilder, timestampValue); if (!string.IsNullOrEmpty(notification.Group)) { s_Jni.NotificationBuilder.SetGroup(notificationBuilder, notification.Group); } if (notification.GroupSummary) { s_Jni.NotificationBuilder.SetGroupSummary(notificationBuilder, notification.GroupSummary); } if (!string.IsNullOrEmpty(notification.SortKey)) { s_Jni.NotificationBuilder.SetSortKey(notificationBuilder, notification.SortKey); } if (notification.ShowTimestamp) { s_Jni.NotificationBuilder.SetShowWhen(notificationBuilder, notification.ShowTimestamp); } int color = notification.Color.ToInt(); if (color != 0) { s_Jni.NotificationManager.SetNotificationColor(notificationBuilder, color); } if (notification.UsesStopwatch) { s_Jni.NotificationManager.SetNotificationUsesChronometer(notificationBuilder, notification.UsesStopwatch); } if (notification.GroupAlertBehaviour != GroupAlertBehaviours.GroupAlertAll) // All is default value { s_Jni.NotificationManager.SetNotificationGroupAlertBehavior(notificationBuilder, (int)notification.GroupAlertBehaviour); } extras = s_Jni.NotificationBuilder.GetExtras(notificationBuilder); s_Jni.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, notification.RepeatInterval.ToLong()); s_Jni.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, fireTime); s_Jni.Bundle.PutBoolean(extras, s_Jni.NotificationManager.KEY_SHOW_IN_FOREGROUND, notification.ShowInForeground); if (!string.IsNullOrEmpty(notification.IntentData)) { s_Jni.Bundle.PutString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA, notification.IntentData); } }