Generate Notification Key In Android

07.12.2020by
  • Security keys are a more secure second step. If you have other second steps set up, use your security key to sign in whenever possible. If a security key doesn't work on your device or browser, you might see an option to sign in with a code or prompt instead.
  • Sending/displaying a notification is one of the easy things to do in Android. To create and display an Android notification, all you have to do is: Create the title text (ticker text) to display in the status bar when the notification is shown. Use an icon to show in the status bar after the notification goes away.
Android

In this article, we are going to generate a push notification in our android device with the help of NotificationManager class. Submitted by Manu Jemini, on February 09, 2018 In the example below, we are going to use the NotificationManager and Notification class to create and show the notification.

  • Android Basics
  • Android - User Interface
  • Android Advanced Concepts
  • Android Useful Examples
  • Android Useful Resources
  • Selected Reading

A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

Android Toast class provides a handy way to show users alerts but problem is that these alerts are not persistent which means alert flashes on the screen for a few seconds and then disappears.

To see the details of the notification, you will have to select the icon which will display notification drawer having detail about the notification. While working with emulator with virtual device, you will have to click and drag down the status bar to expand it which will give you detail as follows. This will be just 64 dp tall and called normal view.

Above expanded form can have a Big View which will have additional detail about the notification. You can add upto six additional lines in the notification. The following screen shot shows such notification.

Create and Send Notifications

You have simple way to create a notification. Follow the following steps in your application to create a notification −

Step 1 - Create Notification Builder

As a first step is to create a notification builder using NotificationCompat.Builder.build(). You will use Notification Builder to set various Notification properties like its small and large icons, title, priority etc.

Step 2 - Setting Notification Properties

Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following −

  • A small icon, set by setSmallIcon()

  • A title, set by setContentTitle()

  • Detail text, set by setContentText()

You have plenty of optional properties which you can set for your notification. To learn more about them, see the reference documentation for NotificationCompat.Builder.

Step 3 - Attach Actions

This is an optional part and required if you want to attach an action with the notification. An action allows users to go directly from the notification to an Activity in your application, where they can look at one or more events or do further work.

The action is defined by a PendingIntent containing an Intent that starts an Activity in your application. To associate the PendingIntent with a gesture, call the appropriate method of NotificationCompat.Builder. For example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

A PendingIntent object helps you to perform an action on your applications behalf, often at a later time, without caring of whether or not your application is running.

We take help of stack builder object which will contain an artificial back stack for the started Activity. This ensures that navigating backward from the Activity leads out of your application to the Home screen.

Step 4 - Issue the notification

Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you call NotificationCompat.Builder.build() method on builder object before notifying it. This method combines all of the options that have been set and return a new Notification object.

The NotificationCompat.Builder Class

The NotificationCompat.Builder class allows easier control over all the flags, as well as help constructing the typical notification layouts. Following are few important and most frequently used methods available as a part of NotificationCompat.Builder class.

Sr.No.Constants & Description
1

Notification build()

Combine all of the options that have been set and return a new Notification object.

2

NotificationCompat.Builder setAutoCancel (boolean autoCancel)

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.

3

NotificationCompat.Builder setContent (RemoteViews views)

Supply a custom RemoteViews to use instead of the standard one.

4

NotificationCompat.Builder setContentInfo (CharSequence info)

Set the large text at the right-hand side of the notification.

5

NotificationCompat.Builder setContentIntent (PendingIntent intent)

Supply a PendingIntent to send when the notification is clicked.

6

NotificationCompat.Builder setContentText (CharSequence text)

Set the text (second row) of the notification, in a standard notification.

7

NotificationCompat.Builder setContentTitle (CharSequence title)

Set the text (first row) of the notification, in a standard notification.

8

NotificationCompat.Builder setDefaults (int defaults)

Set the default notification options that will be used.

9

NotificationCompat.Builder setLargeIcon (Bitmap icon)

Set the large icon that is shown in the ticker and notification.

10

NotificationCompat.Builder setNumber (int number)

Set the large number at the right-hand side of the notification.

11

NotificationCompat.Builder setOngoing (boolean ongoing)

Set whether this is an ongoing notification.

12

NotificationCompat.Builder setSmallIcon (int icon)

Set the small icon to use in the notification layouts.

13

NotificationCompat.Builder setStyle (NotificationCompat.Style style)

Add a rich notification style to be applied at build time.

14

NotificationCompat.Builder setTicker (CharSequence tickerText)

Set the text that is displayed in the status bar when the notification first arrives.

15

NotificationCompat.Builder setVibrate (long[] pattern)

Set the vibration pattern to use.

16

NotificationCompat.Builder setWhen (long when)

Set the time that the event occurred. Notifications in the panel are sorted by this time.

Example

Following example shows the functionality of a Android notification using a NotificationCompat.Builder Class which has been introduced in Android 4.1.

StepDescription
1You will use Android studio IDE to create an Android application and name it as tutorialspoint under a package com.example.notificationdemo.
2Modify src/MainActivity.java file and add the code to notify('), if user click on the button,it will call android notification service.
3Create a new Java file src/NotificationView.java, which will be used to display new layout as a part of new activity which will be started when user will click any of the notifications
4Modify layout XML file res/layout/activity_main.xml to add Notification button in relative layout.
5Create a new layout XML file res/layout/notification.xml. This will be used as layout file for new activity which will start when user will click any of the notifications.
6No need to change default string constants. Android studio takes care of default string constants
7Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.notificationdemo/MainActivity.java. This file can include each of the fundamental lifecycle methods.

Following will be the content of res/layout/notification.xml file −

Key

Following is the content of the modified main activity file src/com.example.notificationdemo/NotificationView.java.

Following will be the content of res/layout/activity_main.xml file −

Following will be the content of res/values/strings.xml to define two new constants −

Following is the default content of AndroidManifest.xml

Let's try to run your tutorialspoint application. I assume you had created your AVD while doing environment set-up. To run the APP from Android Studio, open one of your project's activity files and click Run icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

Free rider 2 mac download. Now click button, you will see at the top a message 'New Message Alert!' will display momentarily and after that you will have following screen having a small icon at the top left corner.

Now lets expand the view, long click on the small icon, after a second it will display date information and this is the time when you should drag status bar down without releasing mouse. You will see status bar will expand and you will get following screen −

Big View Notification

The following code snippet demonstrates how to alter the notification created in the previous snippet to use the Inbox big view style. I'm going to update displayNotification() modification method to show this functionality −

Now if you will try to run your application then you will find following result in expanded form of the view −

The second Android 11 Developer Preview is now available, test it out and share your feedback.

Android Studio includes keyboard shortcuts for many common actions. Table 1shows the default keyboard shortcuts by operating system. Keep in mind, becauseAndroid Studio is based on IntelliJ IDEA, you can find additional shortcuts inthe IntelliJ IDEA keymap reference documentation.

Generate Notification Key In Android App

Note: In addition to the default keymaps intable 1 below, you can select from a number of preset keymaps or create a customkeymap. For more about customizing your keyboard shortcuts, seeConfigure Custom Keymaps, below.

Table 1. Default keyboard shortcuts for Windows/Linux and Mac operating systems.

DescriptionWindows/LinuxMac
General
Save allControl+SCommand+S
SynchronizeControl+Alt+YCommand+Option+Y
Maximize/minimize editorControl+Shift+F12Control+Command+F12
Add to favoritesAlt+Shift+FOption+Shift+F
Inspect current file with current profileAlt+Shift+IOption+Shift+I
Quick switch schemeControl+` (backquote)Control+` (backquote)
Open settings dialogueControl+Alt+SCommand+, (comma)
Open project structure dialogControl+Alt+Shift+SCommand+; (semicolon)
Switch between tabs and tool windowControl+TabControl+Tab
Navigating and searching within Studio
Search everything (including code and menus)Press Shift twicePress Shift twice
FindControl+FCommand+F
Find nextF3Command+G
Find previousShift+F3Command+Shift+G
ReplaceControl+RCommand+R
Find actionControl+Shift+ACommand+Shift+A
Search by symbol nameControl+Alt+Shift+NCommand+Option+O
Find classControl+NCommand+O
Find file (instead of class)Control+Shift+NCommand+Shift+O
Find in pathControl+Shift+FCommand+Shift+F
Open file structure pop-upControl+F12Command+F12
Navigate between open editor tabsAlt+Right Arrow or Left ArrowControl+Right Arrow or Control+Left Arrow
Jump to sourceF4 or Control+EnterF4 or Command+Down Arrow
Open current editor tab in new windowShift+F4Shift+F4
Recently opened files pop-upControl+ECommand+E
Recently edited files pop-upControl+Shift+ECommand+Shift+E
Go to last edit locationControl+Shift+BackspaceCommand+Shift+Delete
Close active editor tabControl+F4Command+W
Return to editor window from a tool windowEscEsc
Hide active or last active tool windowShift+EscShift+Esc
Go to lineControl+GCommand+L
Open type hierarchyControl+HControl+H
Open method hierarchyControl+Shift+HCommand+Shift+H
Open call hierarchyControl+Alt+HControl+Option+H
Viewing layouts
Zoom in/outControl+plus or Control+minusCommand+plus or Command+minus
Fit to screenControl+0Command+0
Actual sizeControl+Shift+1Command+Shift+1
Design tools: Layout Editor
Toggle between Design and Blueprint modesBB
Toggle between Portrait and Landscape modesOO
Toggle devicesDD
Force refreshRR
Toggle render errors panelEE
Delete constraintsDelete or Control+clickDelete or Command+click
Zoom inControl+plusCommand+plus
Zoom outControl+minusCommand+minus
Zoom to fitControl+0Command+0
PanHold Space+click and dragHold Space+click and drag
Go to XMLControl+BCommand+B
Select all componentsControl+ACommand+A
Select multiple componentsShift+click or Control+clickShift+click or Command+click
Design tools: Navigation Editor
Zoom inControl+plusCommand+plus
Zoom outControl+minusCommand+minus
Zoom to fitControl+0Command+0
PanHold Space+click and dragHold Space+click and drag
Go to XMLControl+BCommand+B
Toggle render errors panelEE
Group into nested graphControl+GCommand+G
Cycle through destinationsTab or Shift+TabTab or Shift+Tab
Select all destinationsControl+ACommand+A
Select multiple destinationsShift+click or Control+clickShift+click or Command+click
Writing code
Generate code (getters, setters, constructors, hashCode/equals, toString, new file, new class)Alt+InsertCommand+N
Override methodsControl+OControl+O
Implement methodsControl+IControl+I
Surround with (if..else / try..catch / etc.)Control+Alt+TCommand+Option+T
Delete line at caretControl+YCommand+Delete
Collapse/expand current code blockControl+minus or Control+plusCommand+minus or Command+plus
Collapse/expand all code blocksControl+Shift+minus or Control+Shift+plusCommand+Shift+minus or Command+Shift+plus
Duplicate current line or selectionControl+DCommand+D
Basic code completionControl+SpaceControl+Space
Smart code completion (filters the list of methods and variables by expected type)Control+Shift+SpaceControl+Shift+Space
Complete statementControl+Shift+EnterCommand+Shift+Enter
Quick documentation lookupControl+QControl+J
Show parameters for selected methodControl+PCommand+P
Go to declaration (directly)Control+B or Control+clickCommand+B or Command+click
Go to implementationsControl+Alt+BCommand+Option+B
Go to super-method/super-classControl+UCommand+U
Open quick definition lookupControl+Shift+ICommand+Y
Toggle project tool window visibilityAlt+1Command+1
Toggle bookmarkF11F3
Toggle bookmark with mnemonicControl+F11Option+F3
Comment/uncomment with line commentControl+/Command+/
Comment/uncomment with block commentControl+Shift+/Command+Shift+/
Select successively increasing code blocksControl+WOption+Up
Decrease current selection to previous stateControl+Shift+WOption+Down
Move to code block startControl+[Option+Command+[
Move to code block endControl+]Option+Command+]
Select to the code block startControl+Shift+[Option+Command+Shift+[
Select to the code block endControl+Shift+]Option+Command+Shift+]
Delete to end of wordControl+DeleteOption+Delete
Delete to start of wordControl+BackspaceOption+Delete
Optimize importsControl+Alt+OControl+Option+O
Project quick fix (show intention actions and quick fixes)Alt+EnterOption+Enter
Reformat codeControl+Alt+LCommand+Option+L
Auto-indent linesControl+Alt+IControl+Option+I
Indent/unindent linesTab or Shift+TabTab or Shift+Tab
Smart line joinControl+Shift+JControl+Shift+J
Smart line splitControl+EnterCommand+Enter
Start new lineShift+EnterShift+Enter
Next/previous highlighted errorF2 or Shift+F2F2 or Shift+F2
Build and run
BuildControl+F9Command+F9
Build and runShift+F10Control+R
Apply Changes and Restart ActivityControl+F10Control+Command+R
Apply Code ChangesControl+Alt+F10Control+Shift+Command+R
Debugging
DebugShift+F9Control+D
Step overF8F8
Step intoF7F7
Smart step intoShift+F7Shift+F7
Step outShift+F8Shift+F8
Run to cursorAlt+F9Option+F9
Evaluate expressionAlt+F8Option+F8
Resume programF9Command+Option+R
Toggle breakpointControl+F8Command+F8
View breakpointsControl+Shift+F8Command+Shift+F8
Refactoring
CopyF5F5
MoveF6F6
Safe deleteAlt+DeleteCommand+Delete
RenameShift+F6Shift+F6
Change signatureControl+F6Command+F6
InlineControl+Alt+NCommand+Option+N
Extract methodControl+Alt+MCommand+Option+M
Extract variableControl+Alt+VCommand+Option+V
Extract fieldControl+Alt+FCommand+Option+F
Extract constantControl+Alt+CCommand+Option+C
Extract parameterControl+Alt+PCommand+Option+P
Version control / local history
Commit project to VCSControl+KCommand+K
Update project from VCSControl+TCommand+T
View recent changesAlt+Shift+COption+Shift+C
Open VCS popupAlt+` (backquote)Control+V

Configure custom keymaps

You can choose from a number of preset keymaps or modify a preset keymap tocreate a new custom keymap in the keymap settings for Android Studio.

Android Developer Notifications

To open the keymap settings, choose File > Settings (on Mac, AndroidStudio > Preferences) and navigate to the Keymap pane.

Figure 1. The Android Studio keymap settings window.

  1. Keymaps dropdown: Select the desired keymap from this menu to switch between preset keymaps.
  2. Actions list: Right click on an action to modify it. You can add additional keyboard shortcuts for the action, add mouse shortcuts to associate an action with a mouse click, or remove current shortcuts. If you are using a preset keymap, modifying an action’s shortcuts will automatically create a copy of the keymap and add your modifications to the copy.
  3. Copy button: Select a keymap from the dropdown menu to use as a starting point, and click Copy to create a new custom keymap. You can modify the keymap name and shortcuts.
  4. Reset button: Select a keymap from the dropdown menu and click Reset to revert it to its original configuration.
  5. Search box: Type here to search for a keyboard shortcut by the action name.
  6. Search by Shortcut: Click Find Actions by Shortcut and type a shortcut to search for actions by shortcut.
Comments are closed.