Displaying Notifications
Displaying Notifications
2
Notifications
What is a Notification?
Notification shown
on the status line
Drag down
Click on
Notification
Panel to
execute
associated
application
3
Notifications
Notification Manager
This class notifies the user of events that happen in the background.
1. A persistent icon that goes in the status bar and is accessible through the
launcher, (when the user selects it, a designated Intent can be launched),
4
Notifications – Since Jelly Bean 4.0
Base Layout (points question)
All notifications include in their MINIMUM configurations three parts:
1. the sending application's notification icon or the sender's photo a
notification title and message.
2. a timestamp.
3. a secondary icon to identify the sending application when the senders
image is shown for the main icon.
Optional Entries
4. Additional lines
5. Up to three actions
6. A summary line
5
Notifications
Notification Manager
notificationManager = (NotificationManager)
getSystemService (servName);
6
Notifications(skip)
Notification Builder
A convenient way to set up various fields of a Notification
Example:
7
Notifications(skip)
Example ticker text ko read krna h
8
Notifications(skip)
Example
9
Notifications(skip)
Example
10
Notifications(skip)
Example: MainActivity
package com.example.mynotificationmanager;
import . . .
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btnBig).setOnClickListener(this);
findViewById(R.id.btnCancel).setOnClickListener(this);
}// onCreate
@SuppressLint("NewApi")
public void createBigNotification(View view) {
.addLine("Line-1")
.addLine("Line-2")
.addLine("Line-2")
.setSummaryText("SUMMARY-Line-1 here")
.build();
notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
// notification ID is 12345
notificationManager.notify(12345, noti);
}// createBigNotification
13
@Override
Notifications(skip)
Example: MainActivity
case R.id.btnCancel :
try {
if ( notificationManager != null){
notificationManager.cancel(NOTIFICATION_ID);
}
} catch (Exception e) {
Log.e("<<MAIN>>", e.getMessage() );
}
break;
}
}// onClick
package com.example.mynotificationmanager;
import . . .
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" ><activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".NotificationReceiverActivity"
android:icon="@drawable/icon0" >
16
</activity>
Notifications(skip)
Example: Manifest
<activity android:name=".NotificationReceiverActivity1"
android:icon="@drawable/icon1" >
</activity>
<activity android:name=".NotificationReceiverActivity2"
android:icon="@drawable/icon2" >
</activity>
<activity android:name=".NotificationReceiverActivity3"
android:icon="@drawable/icon3" >
</activity>
</application>
</manifest>
17
Notifications(skip)
Example - Layout: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnBig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Big Notification"
android:ems="20" >
</Button>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Notification"
android:ems="20" >
</Button>
</LinearLayout>
18
Notifications(skip)
Example - Layout: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Called from the MASTER notification" >
</TextView>
</LinearLayout>
19
Notifications (Before SDK 4.0)
Define the notification manager and how a persistent notification is to
be presented to the user using the notification manager? (20)
This class represents how a persistent notification is to be
presented to the user using the NotificationManager.
Parameters
icon The resource id of the icon to put
in the status bar.
tickerText The text that flows by in the status bar when the
notification
first activates.
Parameters
id An identifier for this
notification unique within
your application.
Sets the contentView field to be a view with the standard "Latest Event"
layout.
Parameters
context The context for your application / activity.
contentTitle The title that goes in the expanded entry.
contentText The text that goes in the expanded entry.
contentIntent The intent to launch when the user clicks the expanded
notification.
22
Notifications (Before SDK 4.0)
Notification – Methods
Parameters
Id An identifier for this notification unique within your
application.
23
Notifications (Before SDK 4.0)
Example (no code from this slide till the end).
Produce a notification. Allow the user to click on the Notification Panel
and execute appropriate activity to attend the message.
24
Notifications (Before SDK 4.0)
Example - Layouts
main.xml main2.xml
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
android:id="@+id/myLinearLayout1" android:id="@+id/main2LinLayout"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:background="#ff000066" android:background="#ff660000"
android:orientation="vertical" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" > xmlns:android="http://schemas.android.com/apk/res/
android"
<Button >
android:id="@+id/btnGo"
android:layout_width="106dp" <TextView
android:layout_height="61dp" android:id="@+id/widget29"
android:layout_margin="10dp" android:layout_width="251dp"
android:text=" Show Notification " > android:layout_height="69dp"
</Button> android:text="Hola this is screen 2 - Layout:main2.xml"
<Button >
android:id="@+id/btnStop" </TextView>
android:layout_width="106dp"
android:layout_height="61dp" </LinearLayout>
android:layout_margin="10dp"
android:text=" Cancel Notification " >
</Button>
</LinearLayout>
25
25
Notifications (Before SDK 4.0)
Example – Manifest /drawable
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cis493.demos"
android:versionCode="1"
android:versionName="1.0"> btn_star_big_on_selected.png
<application android:icon="@drawable/icon"
android:label="@string/app_name">
Note:
<activity android:name=".NotifyDemo1"
android:label="@string/app_name">
Obtain the icon from the folder
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
C:\Android\platforms\android-
</intent-filter> 1.x\data\res\drawable
</activity>
</application>
</manifest>
26
Notifications (Before SDK 4.0)
Example – Create & Cancel a Notification
package cis493.demos;
import . . .
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
27
Notifications (Before SDK 4.0)
Example – Create & Cancel a Notification
btnGo = (Button)findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
notification.setLatestEventInfo(getApplicationContext(),
extendedTitle, extendedText,
launchIntent);
//trigger notification
notificationId = 1;
notificationManager.notify(notificationId, notification);
}//click
});
btnStop = (Button)findViewById(R.id.btnStop);
btnStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//canceling a
notification
notificationId = 1;
notificationManager.cancel(notificationId);
} 29
Notifications (Before SDK 4.0)
Example - SubActivity – Attending the Notification
package cis493.demos;
Import. . .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
30
Notifications
Questions
31