Class 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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA single action button attached to a local notification.static classDescribes a conversation (messaging style) notification. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConstant used in#setRepeatType(int)to indicate that this notification should be repeated every day.static final intConstant used in#setRepeatType(int)to indicate that this notification should be repeated every hour.static final intConstant used in#setRepeatType(int)to indicate that this notification should be repeated every 1 minute.static final intConstant used in#setRepeatType(int)to indicate that this notification should not be repeated.static final intConstant used in#setRepeatType(int)to indicate that this notification should be repeated every week. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAction(LocalNotification.Action action) Adds an action button to this notification.Adds a simple action button to this notification.addInputAction(String id, String title, String placeholder, String replyButtonText) Adds a quick reply action with an inline text input field.asMessagingStyle(String selfDisplayName) Configures this notification to render as a conversation (messaging style) notification.Returns the list of action buttons configured on this notification.Gets the alert body to be displayed for this notification.Gets the notification imageGets the alert sound to be sounded when the notification arrives.Gets the alert title to be displayed for this notification.intGets the badge number to set for this notification.Gets the notification channel id this notification is posted to.Gets the custom view layout name used to render this notification.Gets the group id used to bundle related notifications together in the shade.getId()Gets the ID of the notification.Returns the messaging style configured on this notification, or null if this is not a messaging style notification.intReturns the current progress value.intReturns the maximum value of the progress bar, or 0 if no progress bar is shown.booleanChecks whether this notification will be displayed in the device's notification center even when the app is in the foreground.booleanReturns true if this notification should launch a full screen intent.booleanReturns true if this notification is the summary for its group.booleanReturns true if this notification is ongoing.booleanReturns true if the progress bar is indeterminate.booleanReturns true if this notification is marked time sensitive.voidsetAlertBody(String alertBody) Sets the alert body to be displayed for this notification.voidsetAlertImage(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.voidsetAlertSound(String alertSound) Sets the alert sound to be sounded when the notification arrives.voidsetAlertTitle(String alertTitle) Sets the alert title to be displayed for this notification.voidsetBadgeNumber(int badgeNumber) Gets the badge number to set for this notification.setChannelId(String channelId) Sets the notification channel id this notification is posted to.setCustomView(String customViewLayout) Sets a custom view layout name for this notification.voidsetForeground(boolean foreground) Set whether this notification should be displayed in the device's notification center even when the app is in the foreground.setFullScreenIntent(boolean fullScreenIntent) Requests that this notification launch a full screen intent (Android), used for high priority interruptions such as incoming calls or alarms.Assigns this notification to a group.setGroupSummary(boolean groupSummary) Marks this notification as the summary of its group (Android).voidSets the ID of the notification.setIndeterminateProgress(boolean indeterminate) Shows an indeterminate (spinning) progress bar on this notification (Android).setOngoing(boolean ongoing) Marks this notification as ongoing (Android), meaning it cannot be dismissed by the user and represents background activity in progress.setProgress(int max, int current) Shows a determinate progress bar on this notification (Android).Convenience alias for#setAlertSound(String)that returns this notification for chaining.setTimeSensitive(boolean timeSensitive) Marks this notification as time sensitive so it can break through Focus modes (iOS) or be treated with elevated importance (Android).
-
Field Details
-
REPEAT_NONE
public static final int REPEAT_NONEConstant used in#setRepeatType(int)to indicate that this notification should not be repeated.- See Also:
-
REPEAT_MINUTE
public static final int REPEAT_MINUTEConstant used in#setRepeatType(int)to indicate that this notification should be repeated every 1 minute.- See Also:
-
REPEAT_HOUR
public static final int REPEAT_HOURConstant used in#setRepeatType(int)to indicate that this notification should be repeated every hour.- See Also:
-
REPEAT_DAY
public static final int REPEAT_DAYConstant used in#setRepeatType(int)to indicate that this notification should be repeated every day.- See Also:
-
REPEAT_WEEK
public static final int REPEAT_WEEKConstant used in#setRepeatType(int)to indicate that this notification should be repeated every week.- See Also:
-
-
Constructor Details
-
LocalNotification
public LocalNotification()
-
-
Method Details
-
getBadgeNumber
public int getBadgeNumber()Gets the badge number to set for this notification.
Returns
the badgeNumber
-
setBadgeNumber
public void setBadgeNumber(int badgeNumber) Gets the badge number to set for this notification.
Parameters
badgeNumber: the badgeNumber to set
-
getAlertBody
Gets the alert body to be displayed for this notification.
Returns
the alertBody
-
setAlertBody
Sets the alert body to be displayed for this notification.
Parameters
alertBody: the alertBody to set
-
getAlertTitle
Gets the alert title to be displayed for this notification.
Returns
the alertTitle
-
setAlertTitle
Sets the alert title to be displayed for this notification.
Parameters
alertTitle: the alertTitle to set
-
getAlertSound
Gets 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.
Returns
the alertSound
-
setAlertSound
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
alertSound: the alertSound to set
-
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
the id
-
setId
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
id: the id to set
-
getAlertImage
Gets the notification image
Returns
image path
-
setAlertImage
Sets an image to be displayed on the platform notifications bar, if the underlying platform supports image displaying otherwise the image will be ignored.
Parameters
image: a path to the image, the image needs to be placed in the app root.
-
isForeground
public boolean isForeground()Checks whether this notification will be displayed in the device's notification center even when the app is in the foreground.
Returns
True if the notification will display in the device's notification center even when the app is in the foreground.
Since
7.0
-
setForeground
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.
Parameters
foreground: True to display this notification in the notification center even when the app is in the foreground.
Since
7.0
-
getChannelId
Gets the notification channel id this notification is posted to. Channels are an Android concept; see
NotificationChannelBuilder. On platforms without channels this value is ignored.Returns
the channel id, or null
-
setChannelId
Sets the notification channel id this notification is posted to.
Parameters
channelId: the channel id
Returns
this notification for chaining
-
setSound
Convenience alias for
#setAlertSound(String)that returns this notification for chaining.Parameters
sound: the alert sound file path
Returns
this notification for chaining
-
getGroupId
Gets the group id used to bundle related notifications together in the shade.
Returns
the group id, or null
-
setGroup
Assigns this notification to a group. Notifications sharing a group id are visually bundled. On iOS the group id maps to the notification thread identifier.
Parameters
groupId: the group id
Returns
this notification for chaining
-
isGroupSummary
public boolean isGroupSummary()Returns true if this notification is the summary for its group.
Returns
true if this is a group summary
-
setGroupSummary
Marks this notification as the summary of its group (Android). The summary is the single entry shown when the group is collapsed.
Parameters
groupSummary: true to make this notification the group summary
Returns
this notification for chaining
-
isFullScreenIntent
public boolean isFullScreenIntent()Returns true if this notification should launch a full screen intent.
Returns
true if a full screen intent is requested
-
setFullScreenIntent
Requests that this notification launch a full screen intent (Android), used for high priority interruptions such as incoming calls or alarms. Ignored on platforms that do not support it.
Parameters
fullScreenIntent: true to request a full screen intent
Returns
this notification for chaining
-
isTimeSensitive
public boolean isTimeSensitive()Returns true if this notification is marked time sensitive.
Returns
true if time sensitive
-
setTimeSensitive
Marks this notification as time sensitive so it can break through Focus modes (iOS) or be treated with elevated importance (Android). Requires the corresponding permission to have been requested.
Parameters
timeSensitive: true to mark the notification time sensitive
Returns
this notification for chaining
-
isOngoing
public boolean isOngoing()Returns true if this notification is ongoing.
Returns
true if ongoing
-
setOngoing
Marks this notification as ongoing (Android), meaning it cannot be dismissed by the user and represents background activity in progress. Ignored on platforms that do not support it.
Parameters
ongoing: true to make the notification ongoing
Returns
this notification for chaining
-
getProgressMax
public int getProgressMax()Returns the maximum value of the progress bar, or 0 if no progress bar is shown.
Returns
the progress maximum
-
getProgress
public int getProgress()Returns the current progress value.
Returns
the current progress
-
setProgress
Shows a determinate progress bar on this notification (Android).
Parameters
-
max: the maximum progress value -
current: the current progress value
Returns
this notification for chaining
-
-
isProgressIndeterminate
public boolean isProgressIndeterminate()Returns true if the progress bar is indeterminate.
Returns
true if the progress bar is indeterminate
-
setIndeterminateProgress
Shows an indeterminate (spinning) progress bar on this notification (Android).
Parameters
indeterminate: true to show an indeterminate progress bar
Returns
this notification for chaining
-
getCustomView
Gets the custom view layout name used to render this notification.
Returns
the custom view layout name, or null
-
setCustomView
Sets a custom view layout name for this notification. On Android this maps to a RemoteViews layout bundled in the native resources. On iOS a custom view is rendered by a notification content extension keyed by the notification category. Ignored on platforms that do not support custom notification views.
Parameters
customViewLayout: the layout name
Returns
this notification for chaining
-
addAction
Adds an action button to this notification.
Parameters
action: the action to add
Returns
this notification for chaining
-
addAction
Adds a simple action button to this notification.
Parameters
-
id: the action id reported back when the user taps the action -
title: the button label
Returns
this notification for chaining
-
-
addInputAction
public LocalNotification addInputAction(String id, String title, String placeholder, String replyButtonText) Adds a quick reply action with an inline text input field. When the user submits a reply the entered text is reported back via
com.codename1.push.PushContent#getTextResponse()alongside the action id.Parameters
-
id: the action id reported back when the user submits the reply -
title: the button label -
placeholder: placeholder text shown in the text input field -
replyButtonText: the label for the send button
Returns
this notification for chaining
-
-
getActions
Returns the list of action buttons configured on this notification.
Returns
the actions, never null
-
asMessagingStyle
Configures this notification to render as a conversation (messaging style) notification. Returns the
MessagingStyleso messages can be added fluently.Parameters
selfDisplayName: the name representing the device user in the conversation
Returns
the messaging style for further configuration
-
getMessagingStyle
Returns the messaging style configured on this notification, or null if this is not a messaging style notification.
Returns
the messaging style, or null
-