{"id":2564,"date":"2017-05-10T10:32:04","date_gmt":"2017-05-10T10:32:04","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2564"},"modified":"2017-05-10T10:32:04","modified_gmt":"2017-05-10T10:32:04","slug":"how-to-create-notification-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-create-notification-in-android\/","title":{"rendered":"How to Create Notification in Android"},"content":{"rendered":"<p>In this tutorial how to use Create Notification for Android Application using NotificationManager in Android is shown.<\/p>\n<p>Notification means an alert message for user to display the information regarding software update, email, messaging on Status Bar.<\/p>\n<p>The Android platform provides different ways of notifying the user by using sound, message, icon, blink light etc.<\/p>\n<p>Status Bar is the standard location to indicate notification.<\/p>\n<p>A simple notification has number important components<\/p>\n<ul>\n<li>An icon<\/li>\n<li>Ticker text<\/li>\n<li>Notification title text<\/li>\n<li>Notification body text<\/li>\n<li>An intent<\/li>\n<\/ul>\n<p>To create the notification, use the object of NotificationManager class.<\/p>\n<p>To create the object of NotificationManager call getSystemService() method.<\/p>\n<p>NotificationCompat.Builder class provide the wide range to create notification<\/p>\n<p>To define notification action we have to handle Intent and PendingIntent.<\/p>\n<p>Call notify() method to issue the notification, it will specify the notification id and also call build() method which will return Notification object.<\/p>\n<p>Code:<\/p>\n<p>main_activity.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/tv1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;106dp&#8221;<br \/>\nandroid:text=&#8221;Notification Program&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.os.Vibrator;<br \/>\nimport android.app.Activity;<br \/>\nimport android.app.NotificationManager;<br \/>\nimport android.app.PendingIntent;<br \/>\nimport android.content.Context;<br \/>\nimport android.content.Intent;<br \/>\nimport android.graphics.Color;<br \/>\nimport android.support.v4.app.NotificationCompat;<br \/>\nimport android.view.Menu;<\/p>\n<p>public class MainActivity extends Activity{<\/p>\n<p>static int num=0;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nNotificationCompat.Builder ncb=new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(&#8220;Hello!! &#8220;).setContentText(&#8220;This is a notification&#8221;).setLights(Color.GREEN, 10, 10).setNumber(++num).setAutoCancel(true);<\/p>\n<p>Intent in=new Intent(this,secActivity.class);<br \/>\nPendingIntent pi=PendingIntent.getActivity(this,0,in,PendingIntent.FLAG_UPDATE_CURRENT);<\/p>\n<p>ncb.setContentIntent(pi);<\/p>\n<p>NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);<br \/>\nnm.notify(001,ncb.build());<\/p>\n<p>Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);<br \/>\nlong[] pat={0,300,500,200};<br \/>\nv.vibrate(pat,-1);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<br \/>\n}<\/p>\n<p>sec_activity.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221; &gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;122dp&#8221;<br \/>\nandroid:text=&#8221;This page is loaded on activity click&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>secActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.app.Activity;<br \/>\nimport android.os.Bundle;<\/p>\n<p>public class secActivity extends Activity {<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.sec_activity);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>AndroidManifest.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;manifest xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\npackage=&#8221;com.example.demo&#8221;<br \/>\nandroid:versionCode=&#8221;1&#8243;<br \/>\nandroid:versionName=&#8221;1.0&#8243; &gt;<\/p>\n<p>&lt;uses-sdk<br \/>\nandroid:minSdkVersion=&#8221;8&#8243;<br \/>\nandroid:targetSdkVersion=&#8221;17&#8243; \/&gt;<\/p>\n<p>&lt;uses-permission android:name=&#8221;android.permission.VIBRATE&#8221;\/&gt;<\/p>\n<p>&lt;application<br \/>\nandroid:allowBackup=&#8221;true&#8221;<br \/>\nandroid:icon=&#8221;@drawable\/ic_launcher&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221;<br \/>\nandroid:theme=&#8221;@style\/AppTheme&#8221; &gt;<br \/>\n&lt;activity<br \/>\nandroid:name=&#8221;com.example.demo.MainActivity&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221; &gt;<br \/>\n&lt;intent-filter&gt;<br \/>\n&lt;action android:name=&#8221;android.intent.action.MAIN&#8221; \/&gt;<\/p>\n<p>&lt;category android:name=&#8221;android.intent.category.LAUNCHER&#8221; \/&gt;<br \/>\n&lt;\/intent-filter&gt;<br \/>\n&lt;\/activity&gt;<br \/>\n&lt;activity android:name=&#8221;.secActivity&#8221;&gt;&lt;\/activity&gt;<br \/>\n&lt;\/application&gt;<\/p>\n<p>&lt;\/manifest&gt;<\/p>\n<p><a href=\"https:\/\/youtu.be\/p8xfuj3RPvs\">https:\/\/youtu.be\/p8xfuj3RPvs<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to use Create Notification for Android Application using NotificationManager in Android is shown. Notification means an alert message for user to display the information regarding software update, email, messaging on Status Bar. The Android platform provides different ways of notifying the user by using sound, message, icon, blink light etc. Status Bar is the standard location to indicate notification. A simple notification has number important components An icon Ticker text Notification title text Notification body text An intent To create the notification, use the object of NotificationManager class. To create the object of NotificationManager call getSystemService() method. NotificationCompat.Builder class provide the wide range to create notification To define notification action we have to handle Intent and <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[122],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2564"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2564"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2564\/revisions"}],"predecessor-version":[{"id":2566,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2564\/revisions\/2566"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}