use of android.support.v4.app.NotificationCompat in project Android-Developers-Samples by johnjohndoe.
the class MainActivity method createNotification.
/**
* This sample demonstrates notifications with custom content views.
*
* <p>On API level 16 and above a big content view is also defined that is used for the
* 'expanded' notification. The notification is created by the NotificationCompat.Builder.
* The expanded content view is set directly on the {@link android.app.Notification} once it has been build.
* (See {@link android.app.Notification#bigContentView}.) </p>
*
* <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout
* definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p>
*/
private void createNotification() {
// BEGIN_INCLUDE(notificationCompat)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// END_INCLUDE(notificationCompat)
// BEGIN_INCLUDE(intent)
//Create Intent to launch this Activity again if the notification is clicked.
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
// END_INCLUDE(intent)
// BEGIN_INCLUDE(ticker)
// Sets the ticker text
builder.setTicker(getResources().getString(R.string.custom_notification));
// Sets the small icon for the ticker
builder.setSmallIcon(R.drawable.ic_stat_custom);
// END_INCLUDE(ticker)
// BEGIN_INCLUDE(buildNotification)
// Cancel the notification when clicked
builder.setAutoCancel(true);
// Build the notification
Notification notification = builder.build();
// END_INCLUDE(buildNotification)
// BEGIN_INCLUDE(customLayout)
// Inflate the notification layout as RemoteViews
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
// Set text on a TextView in the RemoteViews programmatically.
final String time = DateFormat.getTimeInstance().format(new Date()).toString();
final String text = getResources().getString(R.string.collapsed, time);
contentView.setTextViewText(R.id.textView, text);
/* Workaround: Need to set the content view here directly on the notification.
* NotificationCompatBuilder contains a bug that prevents this from working on platform
* versions HoneyComb.
* See https://code.google.com/p/android/issues/detail?id=30495
*/
notification.contentView = contentView;
// big content view set here is displayed.)
if (Build.VERSION.SDK_INT >= 16) {
// Inflate and set the layout for the expanded notification view
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
notification.bigContentView = expandedView;
}
// END_INCLUDE(customLayout)
// START_INCLUDE(notify)
// Use the NotificationManager to show the notification
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, notification);
// END_INCLUDE(notify)
}
use of android.support.v4.app.NotificationCompat in project Android-Developers-Samples by johnjohndoe.
the class MainActivity method sendNotification.
/**
* Send a sample notification using the NotificationCompat API.
*/
public void sendNotification(View view) {
// BEGIN_INCLUDE(build_action)
/** Create an intent that will be fired when the user clicks the notification.
* The intent needs to be packaged into a {@link android.app.PendingIntent} so that the
* notification service can fire it on our behalf.
*/
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// END_INCLUDE(build_action)
// BEGIN_INCLUDE (build_notification)
/**
* Use NotificationCompat.Builder to set up our notification.
*/
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
/** Set the icon that will appear in the notification bar. This icon also appears
* in the lower right hand corner of the notification itself.
*
* Important note: although you can use any drawable as the small icon, Android
* design guidelines state that the icon should be simple and monochrome. Full-color
* bitmaps or busy images don't render well on smaller screens and can end up
* confusing the user.
*/
builder.setSmallIcon(R.drawable.ic_stat_notification);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Set the notification to auto-cancel. This means that the notification will disappear
// after the user taps it, rather than remaining until it's explicitly dismissed.
builder.setAutoCancel(true);
/**
*Build the notification's appearance.
* Set the large icon, which appears on the left of the notification. In this
* sample we'll set the large icon to be the same as our app icon. The app icon is a
* reasonable default if you don't have anything more compelling to use as an icon.
*/
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
/**
* Set the text of the notification. This sample sets the three most commononly used
* text areas:
* 1. The content title, which appears in large type at the top of the notification
* 2. The content text, which appears in smaller text below the title
* 3. The subtext, which appears under the text on newer devices. Devices running
* versions of Android prior to 4.2 will ignore this field, so don't use it for
* anything vital!
*/
builder.setContentTitle("BasicNotifications Sample");
builder.setContentText("Time to learn about notifications!");
builder.setSubText("Tap to view documentation about notifications.");
// END_INCLUDE (build_notification)
// BEGIN_INCLUDE(send_notification)
/**
* Send the notification. This will immediately display the notification icon in the
* notification bar.
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
// END_INCLUDE(send_notification)
}
use of android.support.v4.app.NotificationCompat in project Android-Wear-Codelab by fnk0.
the class MainActivity method sendNotification.
// Define the method to send the notifications with the same name from the Android onClick from the XML Layout
public void sendNotification(View view) {
// id- An identifier for this notification unique within your application.
int notificationId = 001;
// Common elements for all our notifications
String eventTitle = "Sample Notification";
String eventText = "Text for the notification.";
String intentExtra = "This is an extra String!";
String eventDescription = "This is supposed to be a content that will not fit the normal content screen" + " usually a bigger text, by example a long text message or email.";
// Build intent for notification content
Intent viewIntent = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
NotificationCompat.Builder mBuilder = null;
NotificationCompat mNotification = null;
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
switch(view.getId()) {
case R.id.simpleNotification:
mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_wear_notification).setContentTitle(eventTitle).setContentText(eventText).setAutoCancel(// This flag makes the notification disappear when the user clicks on it!
true).setContentIntent(viewPendingIntent);
break;
case R.id.bigNotification:
// bigText will override setContentText
bigStyle.bigText(eventDescription);
// bigContentTitle Override the contentTitle
bigStyle.setBigContentTitle("Override Title");
mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_wear_notification).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_sample_codelab)).setContentTitle(// This is unnecessary for the big notification if you use bigText
eventTitle).setContentText(// Unnecessary if setBigContentTitle is Overriden
eventText).setContentIntent(viewPendingIntent).setAutoCancel(true).setStyle(bigStyle);
break;
case R.id.bigNotificationWithAction:
// Intent pointing to our second activity
Intent photoIntent = new Intent(this, SecondActivity.class);
// Set the extra message that will open in the next activity
photoIntent.putExtra("message", intentExtra);
// Send the photo to the next activity
photoIntent.putExtra("photo", R.drawable.ic_sample_codelab);
// set a new pending intent
PendingIntent photoPending = PendingIntent.getActivity(this, 0, photoIntent, 0);
bigStyle.setBigContentTitle("Mr. Flowers");
bigStyle.bigText("Check out this picture!! :D");
mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_wear_notification).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_sample_codelab)).setContentIntent(// This will be the default OPEN button.
viewPendingIntent).addAction(R.drawable.ic_photo, "See Photo", // This is our extra action. With an Extra Icon and pointing to the other PendingIntent
photoPending).setAutoCancel(true).setStyle(bigStyle);
break;
case R.id.sendCustomNotification:
mBuilder = new NotificationCompat.Builder(this).setSmallIcon(mCustomIcon).setContentTitle(mCustomTitle.getText().toString()).setContentText(mCustomMessage.getText().toString()).setAutoCancel(true).setContentIntent(viewPendingIntent);
// This is an example of the NEW WearableNotification SDK.
// The WearableNotification has special functionality for wearable devices
// By example the setHintHideIcon hides the APP ICON from the notification.
// This code is now Up to date thanks to Romin Irani!! Thanks!
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(mBuilder.build());
wearableExtender.setHintHideIcon(!showIcon);
wearableExtender.extend(mBuilder);
break;
}
notificationManager.notify(notificationId, mBuilder.build());
}
Aggregations