Is there any option to kill/hide/cancel current Toast Notification under Android 10 + TNS?
I want to show the meaning of some words by tapping on them, but under Android 10 user needs to wait until previous Toast Notification times out until he/she can see the next one.
The answer is found!
just store the Toast.makeText( var ) in a variabile like myToast and use cancel() function to remove it.
let myToast = Toast.makeText( var ); // show() function shouldn't be used here!
myToast.show();
...
myToast.cancel();
Related
I am currently designing an extension to make the notifications in the notification section of the calendar expendable. The goal is to make the noficiation expand like the initial notification on the desktop does. I have changed the type of notification added to the noficiation tray to class NotificationBanner from class NotificationMessage. I am currently using a work-around to make this work, this is what my expand function looks like:
expand(animate) {
this.expanded = true;
this._actionBin.visible = this._actionBin.get_n_children() > 0;
if (this._bodyStack.get_n_children() < 2) {
this._expandedLabel = new MessageList.URLHighlighter(this._bodyText,
true, this._useBodyMarkup);
this.setExpandedBody(this._expandedLabel);
}
if (animate) {
if (!this.clickedByButton && !this.forceExpansion) {
// This is the usual way notifications are expanded, using the layout manager
this._bodyStack.ease_property('#layout.expansion', 1, {
progress_mode: Clutter.AnimationMode.EASE_OUT_QUAD,
duration: MessageTray.ANIMATION_TIME,
});
}
else if (this.forceExpansion || this.clickedByButton) {
// When auto expanding or clicked by button, change height of body
oldHeight = this.bodyLabel.get_height();
const lines = Math.ceil(this._bodyText.length / 54);
this.bodyLabel.set_height(lines * this.bodyLabel.get_height());
}
this._actionBin.scale_y = 0;
this._actionBin.ease({
scale_y: 1,
duration: MessageTray.ANIMATION_TIME,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
});
} else {
this._bodyStack.layout_manager.expansion = 1;
this._actionBin.scale_y = 1;
}
this.emit('expanded');
}
As you can see, I have 2 options for this extension: Force expand all notifications or make the user use a button to expand. The current solution is not elegant, it simply changes the height of the notification label which manages the body. Furhermore, the notification body still shows the three dots, implying that the body is still not expanded. I believe this to be an issue with the layout manager, since the proper way to expand is to set message._bodyStack.layout_manager.expansion to 1. That does not work in the case of expanding a message in the notification tray. Is anyone familiar with the layout manager or can help me find a different solution? Here is an image of what my current solution looks like:
Image of an automatically expanded notification in the notification tray due to the extension (note the three dots at the end of the first line being still there)
Okay I have found a solution, it is not related to the layout manager. The value of the message message.bodyLabel.clutter_text.ellipsize is set to 3, which is the main cause of the dots appearing on the notification. Setting this value to 0 solves this problem. I would have still loved to find a more elegant approach to displaying the body, but this will do.
Currently when i create a NSUserNotification using Alert style it won't hide unless i manually close it.
Is there a way i can auto close/hide it say after 2 sec?
NSUserNotification code is for reference :
let notification:NSUserNotification = NSUserNotification()
notification.title = "Title"
notification.subtitle = "Subtitle"
notification.informativeText = "Informative text"
notification.soundName = NSUserNotificationDefaultSoundName
notification.deliveryDate = NSDate(timeIntervalSinceNow: 10)
notification.hasActionButton = false
let notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
notificationcenter.scheduleNotification(notification)
It's actually very simple to do this, using NSObject's
performSelector:withObject:afterDelay: method.
Because you're scheduling the notification delivery after a certain time interval, you need to add the additional delay before dismissing, to the initial delay before delivering. Here, I've written them out as constants of 10 seconds before delivery, and 2 seconds before dismissal:
let delayBeforeDelivering: NSTimeInterval = 10
let delayBeforeDismissing: NSTimeInterval = 2
let notification = NSUserNotification()
notification.title = "Title"
notification.deliveryDate = NSDate(timeIntervalSinceNow: delayBeforeDelivering)
let notificationcenter = NSUserNotificationCenter.defaultUserNotificationCenter()
notificationcenter.scheduleNotification(notification)
notificationcenter.performSelector("removeDeliveredNotification:",
withObject: notification,
afterDelay: (delayBeforeDelivering + delayBeforeDismissing))
And for Swift 5 you can use the following:
let delayBeforeDelivering: TimeInterval = 10
let delayBeforeDismissing: TimeInterval = 2
let notification = NSUserNotification()
notification.title = "Title"
notification.deliveryDate = Date(timeIntervalSinceNow: delayBeforeDelivering)
let notificationcenter = NSUserNotificationCenter.default
notificationcenter.scheduleNotification(notification)
notificationcenter.perform(#selector(NSUserNotificationCenter.removeDeliveredNotification(_:)),
with: notification,
afterDelay: (delayBeforeDelivering + delayBeforeDismissing))
You can use removeDeliveredNotification: or removeAllDeliveredNotifications with timer
// Clear a delivered notification from the notification center. If the notification is not in the delivered list, nothing happens.
- (void)removeDeliveredNotification:(NSUserNotification *)notification;
// Clear all delivered notifications for this application from the notification center.
- (void)removeAllDeliveredNotifications;
OS X (10.8 and later)
Blockquote Is there a way i can auto close/hide it say after 2 sec?
No, you do not have any such option till OSX 10.11, may be in future Apple may provide.
There are three ways a user can customise the NSUserNotification also known as Growl notification:
None
Banner
Alert
You as a developer can not control over the system settings. This is upto the user to enable or disable and choose what kind of notification he likes.
If you want any alert to be shown to user you can create your own alert window and show it in that corner. You can set a timer to close, or provide action button to close it once you need.
Update 1:
Cocoa provides NSWindow & NSPanel(HUD and normal panel). You can either customize Window or panel according to your need. Check there are multiple options that would help you to form as per your requirement.
If you can't get, say you wanted a rounded corner then you need to customize the window/view etc.
When I run this code in ExtendScript Toolkit (Target app - After Effects CC 2015) the cancel button is not responding until the progress bar has finished.
I would like to make it work so that the cancel button would respond immediately.
I know that pressing the "Esc" key works immediately, so it should be possible to make the cancel button work as well.
testWindow = new Window ("palette", "Processing:", undefined);
pb = testWindow.add("progressBar",undefined, 0);
cancelButton = testWindow.add("button", undefined, "Cancel");
testWindow.show();
cancelButton.onClick = function(){
alert("Cancel");
}
i = 1;
while (i <= 1000000) {
pb.value = Math.round(100*i/1000000);
testWindow.update();
i++
}
I think it has always been so: while a script is running, the user interface is on hold and non responding. Adding a Cancel button somewhere cannot work.
The only way i know for a user to cancel the execution of script is the ESC key.
when creating an alarm clock, is it the right way to directly display all scheduled local notifications on a table view? If so, After the notifications have fired they disappear. How can i stop them from disappearing so i can use switches to set them on and off?
What i understood about UIswitch is that i have to cancel the notification when off then reschedule them when on.
#IBAction func addAlarm(sender: UIButton) {
let notification = UILocalNotification()
notification.fireDate = timePicker.date.fireDate
notification.timeZone = NSTimeZone.localTimeZone()
notification.alertBody = "Its time"
notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notification.hasAction = true
notification.alertAction = "View"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
alarmTable.reloadData()
}
Use an array to store the times of the alarms.
Then you can easily show and manage them and create UILocalNotifications based on them.
You have to define the timezone before defining the firedate
I'm using:
var alarm = NSUserNotification()
var currentTime = NSDate()
alarmTime = currentTime.dateByAddingTimeInterval(60)
alarm.deliveryDate = alarmTime
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)
To get a notification that will fire an hour from the current time, the problem is I want the app to automatically set up another alarm after the first one finishes. NSTimer doesn't seem like it will work because once the app goes to the background it kills the timer. What can I do to achieve this? Can I piggy back another method onto a NSUserNotification
Edit: it also needs to be dynamic, I can't just set the repeat variable of the notification because I need to be able to switch how far out the alarm will be each time it resets.
You just have to set your deliveryRepeatInterval and deliveryTimeZone, as follow:
// set your deliveryTimeZone to localTimeZone
alarm.deliveryTimeZone = NSTimeZone.localTimeZone()
// The date components that specify how a notification is to be repeated.
// This value may be nil if the notification should not repeat.
// alarm.deliveryRepeatInterval = nil
// The date component values are relative to the date the notification was delivered.
// If the calendar value of the deliveryRepeatInterval is nil
// the current calendar will be used to calculate the repeat interval.
// alarm.deliveryRepeatInterval?.calendar = NSCalendar.currentCalendar()
// to repeat every day, set .deliveryRepeatInterval.day to 1.
alarm.deliveryRepeatInterval?.day = 1
// to repeat every hour, set .deliveryRepeatInterval.hour to 1.
alarm.deliveryRepeatInterval?.hour = 1
alarm.deliveryDate = NSDate().dateByAddingTimeInterval(60)
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)