Sunday, October 6, 2013

Android-Display Notification

Displaying Notifications

So far, you have been using the Toast class to display messages to the user. While the Toast class is a handy way to show users alerts, it is not persistent. It flashes on the screen for a few seconds and then disappears. If it contains important information, users may easily miss it if they are not looking at the screen.
For messages that are important, you should use a more persistent method. In this case, you should
use the NotificationManager to display a persistent message at the top of the device, commonly known as the status bar (sometimes also referred to as the notification bar). The following Try It Out demonstrates how.
Displaying Notifications on the Status Bar

Displaying Notifications

1. Using Eclipse, create a new Android project and name it Notifications.
2. Add a new class file named NotificationView.java to the src folder of the project (see Figure). In addition, add a new notification.xml file to the res/layout folder as well.


Displaying Notifications

4. Populate the notification.xml file as follows:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Here are the details for the notification...” />

Displaying Notifications

5. Populate the NotificationView.java file as follows:
package net.learn2develop.Notifications;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationView extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
//---look up the notification manager service---
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification that we started
nm.cancel(getIntent().getExtras().getInt(“notificationID”));
}
}

Displaying Notifications

6. Add the following statements in bold to the AndroidManifest.xml file:
package=”net.learn2develop.Notifications”
android:versionCode=”1”
android:versionName=”1.0”>
android:label=”@string/app_name”>
android:label=”Details of notification”>

Displaying Notifications

7. Add the following statements in bold to the main.xml file:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

Displaying Notifications

8. Finally, add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Notifications;
import android.app.Activity;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
int notificationID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.btn_displaynotif);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
displayNotification();
}
});
}
protected void displayNotification()
{
//---PendingIntent to launch activity if the user selects
// this notification---
Intent i = new Intent(this, NotificationView.class);
i.putExtra(“notificationID”, notificationID);
PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.icon,“Reminder: Meeting starts in 5 minutes”,System.currentTimeMillis());
CharSequence from = “System Alarm”;
CharSequence message = “Meeting with customer at 3pm...”;notif.setLatestEventInfo(this, from, message, pendingIntent);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notificationID, notif);
}
}
8. Press F11 to debug the application on the Android Emulator.
9. Click the Display Notification button (see the top left of Figure) and a notification will appearon the status bar.
10. Clicking and dragging the status bar down will reveal the notification (see the right of Figure).
11. Clicking on the notification will reveal the NotificationView activity. This also causes ther.

Displaying Notifications

Displaying Notifications

How It Works
To display a notification, you first created an Intent object to point to the NotificationView class:
//---PendingIntent to launch activity if the user selects
// this notification---
Intent i = new Intent(this, NotificationView.class);
i.putExtra(“notificationID”, notificationID);
This intent will be used to launch another activity when the user selects a notification from the list of notifications.
In this example, you added a key/value pair to the Intent object so that you can tag the notification ID, identifying the notification to the target activity. This ID will be used to dismiss the notifications later.
You would also need to create a PendingIntent object. A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, regardless of whether your application is running or not. In this case, you initialized it as follows:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
The getActivity() method retrieves a PendingIntent object and you set it using the following arguments:
➤ context — Application context
➤ request code — Request code for the intent
➤ intent — The intent for launching the target activity
➤ flags — The flags in which the activity is to be launched
You then obtain an instance of the NotificationManager class and create an instance of the Notification class:
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
“Reminder: Meeting starts in 5 minutes”,
System.currentTimeMillis());
The Notification class enables you to specify the notification’s main information when the notification first appears on the status bar. The second argument to the Notification constructor sets the “ticker text” on the status bar (see Figure)

Next, you set the details of the notification using the setLatestEventInfo() method:
CharSequence from = “System Alarm”;
CharSequence message = “Meeting with customer at 3pm...”;
notif.setLatestEventInfo(this, from, message, pendingIntent);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
The preceding also sets the notification to vibrate the phone. Finally, to display the notification you use
the notify() method:
nm.notify(notificationID, notif);
When the user clicks on the notification, the NotificationView activity is launched. Here, you dismiss the notification by using the cancel() method of the NotificationManager object and passing it the ID
of the notification (passed in via the Intent object):
//---look up the notification manager service---
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification that we started
nm.cancel(getIntent().getExtras().getInt(“notificationID”));




Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...