public class LocalNotification
- Object
- LocalNotification
Local notifications are user notifications that are scheduled by the app itself. They are very similar to push notifications, except that they originate locally, rather than remotely.
They enable an app that isnt running in the foreground to let its users know it has information for them. The information could be a message, an impending calendar event, or new data on a remote server. They can display an alert message or they can badge the app icon. They can also play a sound when the alert or badge number is shown.
When users are notified that the app has a message, event, or other data for them, they can launch the app and see the details. They can also choose to ignore the notification, in which case the app is not activated.
This class encapsulates a single notification (though the notification can be repeating).
Usage
// File: GeofenceListenerImpl.java
public class GeofenceListenerImpl implements GeofenceListener {
@Override
public void onExit(String id) {
}
@Override
public void onEntered(String id) {
if(!Display.getInstance().isMinimized()) {
Display.getInstance().callSerially(() -> {
Dialog.show("Welcome", "Thanks for arriving", "OK", null);
});
} else {
LocalNotification ln = new LocalNotification();
ln.setId("LnMessage");
ln.setAlertTitle("Welcome");
ln.setAlertBody("Thanks for arriving!");
Display.getInstance().scheduleLocalNotification(ln, System.currentTimeMillis() + 10, LocalNotification.REPEAT_NONE);
}
}
}
// File: GeofenceSample.java
Geofence gf = new Geofence("test", loc, 100, 100000);
LocationManager.getLocationManager().addGeoFencing(GeofenceListenerImpl.class, gf);
Android Note: The default image that is used on the android top status bar and on the notification itself is the App’s icon. However Android 5 and above will only display this image as a silhouette using alpha pixels. This will result in many icons appearing to be a blank white square. In such cases you can provide an alternate image to be displayed instead. Place a 24x24 image named “ic_stat_notify.png” in your project’s native/android directory, and this image will be used instead.
Nested types
class LocalNotification.Action | A single action button attached to a local notification. |
class LocalNotification.MessagingStyle | Describes a conversation (messaging style) notification. |
Fields
public static final int REPEAT_NONE = 0 | Constant used in #setRepeatType(int) to indicate that this notification should not be repeated. |
public static final int REPEAT_MINUTE = 1 | Constant used in #setRepeatType(int) to indicate that this notification should be repeated every 1 minute. |
public static final int REPEAT_HOUR = 3 | Constant used in #setRepeatType(int) to indicate that this notification should be repeated every hour. |
public static final int REPEAT_DAY = 4 | Constant used in #setRepeatType(int) to indicate that this notification should be repeated every day. |
public static final int REPEAT_WEEK = 5 | Constant used in #setRepeatType(int) to indicate that this notification should be repeated every week. |
Constructors
public LocalNotification() |
Methods
public int getBadgeNumber() | Gets the badge number to set for this notification. |
public void setBadgeNumber(int badgeNumber) | Gets the badge number to set for this notification. |
public String getAlertBody() | Gets the alert body to be displayed for this notification. |
public void setAlertBody(String alertBody) | Sets the alert body to be displayed for this notification. |
public String getAlertTitle() | Gets the alert title to be displayed for this notification. |
public void setAlertTitle(String alertTitle) | Sets the alert title to be displayed for this notification. |
public String getAlertSound() | Gets the alert sound to be sounded when the notification arrives. |
public void setAlertSound(String alertSound) | Sets the alert sound to be sounded when the notification arrives. |
public String getId() | Gets the ID of the notification. |
public void setId(String id) | Sets the ID of the notification. |
public String getAlertImage() | Gets the notification image |
public void setAlertImage(String image) | Sets an image to be displayed on the platform notifications bar, if the underlying platform supports image displaying otherwise the image will be ignored. |
public boolean isForeground() | Checks whether this notification will be displayed in the device’s notification center even when the app is in the foreground. |
public void setForeground(boolean foreground) | Set whether this notification should be displayed in the device’s notification center even when the app is in the foreground. |
public String getChannelId() | Gets the notification channel id this notification is posted to. |
public LocalNotification setChannelId(String channelId) | Sets the notification channel id this notification is posted to. |
public LocalNotification setSound(String sound) | Convenience alias for #setAlertSound(String) that returns this notification for chaining. |
public String getGroupId() | Gets the group id used to bundle related notifications together in the shade. |
public LocalNotification setGroup(String groupId) | Assigns this notification to a group. |
public boolean isGroupSummary() | Returns true if this notification is the summary for its group. |
public LocalNotification setGroupSummary(boolean groupSummary) | Marks this notification as the summary of its group (Android). |
public boolean isFullScreenIntent() | Returns true if this notification should launch a full screen intent. |
public LocalNotification setFullScreenIntent(boolean fullScreenIntent) | Requests that this notification launch a full screen intent (Android), used for high priority interruptions such as incoming calls or alarms. |
public boolean isTimeSensitive() | Returns true if this notification is marked time sensitive. |
public LocalNotification setTimeSensitive(boolean timeSensitive) | Marks this notification as time sensitive so it can break through Focus modes (iOS) or be treated with elevated importance (Android). |
public boolean isOngoing() | Returns true if this notification is ongoing. |
public LocalNotification setOngoing(boolean ongoing) | Marks this notification as ongoing (Android), meaning it cannot be dismissed by the user and represents background activity in progress. |
public int getProgressMax() | Returns the maximum value of the progress bar, or 0 if no progress bar is shown. |
public int getProgress() | Returns the current progress value. |
public LocalNotification setProgress(int max, int current) | Shows a determinate progress bar on this notification (Android). |
public boolean isProgressIndeterminate() | Returns true if the progress bar is indeterminate. |
public LocalNotification setIndeterminateProgress(boolean indeterminate) | Shows an indeterminate (spinning) progress bar on this notification (Android). |
public String getCustomView() | Gets the custom view layout name used to render this notification. |
public LocalNotification setCustomView(String customViewLayout) | Sets a custom view layout name for this notification. |
public LocalNotification addAction(LocalNotification.Action action) | Adds an action button to this notification. |
public LocalNotification addAction(String id, String title) | Adds a simple action button to this notification. |
public LocalNotification addInputAction(String id, String title, String placeholder, String replyButtonText) | Adds a quick reply action with an inline text input field. |
public List<LocalNotification.Action> getActions() | Returns the list of action buttons configured on this notification. |
public LocalNotification.MessagingStyle asMessagingStyle(String selfDisplayName) | Configures this notification to render as a conversation (messaging style) notification. |
public LocalNotification.MessagingStyle getMessagingStyle() | Returns the messaging style configured on this notification, or null if this is not a messaging style notification. |
Inherited methods
Field details
REPEAT_NONE
public static final int REPEAT_NONE = 0#setRepeatType(int) to indicate that this
notification should not be repeated.REPEAT_MINUTE
public static final int REPEAT_MINUTE = 1#setRepeatType(int) to indicate that this
notification should be repeated every 1 minute.REPEAT_HOUR
public static final int REPEAT_HOUR = 3#setRepeatType(int) to indicate that this
notification should be repeated every hour.REPEAT_DAY
public static final int REPEAT_DAY = 4#setRepeatType(int) to indicate that this
notification should be repeated every day.REPEAT_WEEK
public static final int REPEAT_WEEK = 5#setRepeatType(int) to indicate that this
notification should be repeated every week.Constructor details
LocalNotification
public LocalNotification()Method details
getBadgeNumber
public int getBadgeNumber()Returns
setBadgeNumber
public void setBadgeNumber(int badgeNumber)Parameters
badgeNumberint- the badgeNumber to set
getAlertBody
public String getAlertBody()Returns
setAlertBody
public void setAlertBody(String alertBody)Parameters
alertBodyString- the alertBody to set
getAlertTitle
public String getAlertTitle()Returns
setAlertTitle
public void setAlertTitle(String alertTitle)Parameters
alertTitleString- the alertTitle to set
getAlertSound
public String getAlertSound()Returns
setAlertSound
public void setAlertSound(String alertSound)Sets the alert sound to be sounded when the notification arrives. This should refer to a sound file that is bundled in the default package of your app. The name of the file must start with the “notification_sound” prefix.
java LocalNotification n = new LocalNotification(); n.setAlertSound("/notification_sound_bells.mp3");
Parameters
alertSoundString- the alertSound to set
getId
public String getId()Gets the ID of the notification. The ID is the only information that is
passed to LocalNotificationCallback#localNotificationReceived(java.lang.String)
so you can use it as a lookup key to retrieve the rest of the information as required
from storage or some other mechanism.
The ID can also be used to cancel the notification later using com.codename1.ui.Display#cancelLocalNotification(java.lang.String)
Returns
setId
public void setId(String id)Sets the ID of the notification. The ID is the only information that is
passed to LocalNotificationCallback#localNotificationReceived(java.lang.String)
so you can use it as a lookup key to retrieve the rest of the information as required
from storage or some other mechanism.
The ID can also be used to cancel the notification later using com.codename1.ui.Display#cancelLocalNotification(java.lang.String)
Parameters
idString- the id to set
getAlertImage
public String getAlertImage()Returns
setAlertImage
public void setAlertImage(String image)Parameters
imageString- a path to the image, the image needs to be placed in the app root.
isForeground
public boolean isForeground()Returns
setForeground
public void setForeground(boolean foreground)Parameters
foregroundboolean- True to display this notification in the notification center even when the app is in the foreground.
getChannelId
public String getChannelId()NotificationChannelBuilder. On platforms without channels
this value is ignored.Returns
setChannelId
public LocalNotification setChannelId(String channelId)Parameters
channelIdString- the channel id
Returns
setSound
public LocalNotification setSound(String sound)#setAlertSound(String) that returns this notification for
chaining.Parameters
soundString- the alert sound file path
Returns
getGroupId
public String getGroupId()Returns
setGroup
public LocalNotification setGroup(String groupId)Parameters
groupIdString- the group id
Returns
isGroupSummary
public boolean isGroupSummary()Returns
setGroupSummary
public LocalNotification setGroupSummary(boolean groupSummary)Parameters
groupSummaryboolean- true to make this notification the group summary
Returns
isFullScreenIntent
public boolean isFullScreenIntent()Returns
setFullScreenIntent
public LocalNotification setFullScreenIntent(boolean fullScreenIntent)Parameters
fullScreenIntentboolean- true to request a full screen intent
Returns
isTimeSensitive
public boolean isTimeSensitive()Returns
setTimeSensitive
public LocalNotification setTimeSensitive(boolean timeSensitive)Parameters
timeSensitiveboolean- true to mark the notification time sensitive
Returns
isOngoing
public boolean isOngoing()Returns
setOngoing
public LocalNotification setOngoing(boolean ongoing)Parameters
ongoingboolean- true to make the notification ongoing
Returns
getProgressMax
public int getProgressMax()Returns
getProgress
public int getProgress()Returns
setProgress
public LocalNotification setProgress(int max, int current)Parameters
maxint- the maximum progress value
currentint- the current progress value
Returns
isProgressIndeterminate
public boolean isProgressIndeterminate()Returns
setIndeterminateProgress
public LocalNotification setIndeterminateProgress(boolean indeterminate)Parameters
indeterminateboolean- true to show an indeterminate progress bar
Returns
getCustomView
public String getCustomView()Returns
setCustomView
public LocalNotification setCustomView(String customViewLayout)Parameters
customViewLayoutString- the layout name
Returns
addAction
public LocalNotification addAction(LocalNotification.Action action)Parameters
actionLocalNotification.Action- the action to add
Returns
addAction
public LocalNotification addAction(String id, String title)Parameters
idString- the action id reported back when the user taps the action
titleString- the button label
Returns
addInputAction
public LocalNotification addInputAction(String id, String title, String placeholder, String replyButtonText)com.codename1.push.PushContent#getTextResponse()
alongside the action id.Parameters
idString- the action id reported back when the user submits the reply
titleString- the button label
placeholderString- placeholder text shown in the text input field
replyButtonTextString- the label for the send button
Returns
getActions
public List<LocalNotification.Action> getActions()Returns
asMessagingStyle
public LocalNotification.MessagingStyle asMessagingStyle(String selfDisplayName)MessagingStyle so messages can be added fluently.Parameters
selfDisplayNameString- the name representing the device user in the conversation
Returns
getMessagingStyle
public LocalNotification.MessagingStyle getMessagingStyle()