Search in sources :

Example 1 with NotificationCompat

use of android.support.v7.app.NotificationCompat in project android-play-location by googlesamples.

the class LocationUpdatesService method getNotification.

/**
 * Returns the {@link NotificationCompat} used as part of the foreground service.
 */
private Notification getNotification() {
    Intent intent = new Intent(this, LocationUpdatesService.class);
    CharSequence text = Utils.getLocationText(mLocation);
    // Extra to help us figure out if we arrived in onStartCommand via the notification or not.
    intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);
    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // The PendingIntent to launch activity.
    PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).addAction(R.drawable.ic_launch, getString(R.string.launch_activity), activityPendingIntent).addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates), servicePendingIntent).setContentText(text).setContentTitle(Utils.getLocationTitle(this)).setOngoing(true).setPriority(Notification.PRIORITY_HIGH).setSmallIcon(R.mipmap.ic_launcher).setTicker(text).setWhen(System.currentTimeMillis());
    // Set the Channel ID for Android O.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Channel ID
        builder.setChannelId(CHANNEL_ID);
    }
    return builder.build();
}
Also used : NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 2 with NotificationCompat

use of android.support.v7.app.NotificationCompat in project lzc_app_lib by httplzc.

the class DownLoadNotificationManager method createNotification.

// 初始化通知栏
public void createNotification(Context context, Intent cancelIntent, String uuid, String fileName) {
    if (fileName.length() > maxFileNameLength) {
        fileName = fileName.substring(0, fileName.length() > maxFileNameLength ? maxFileNameLength : fileName.length());
    }
    NotificationData notificationData = new NotificationData(uuid);
    notificationDataHashMap.put(uuid, notificationData);
    notificationData.remoteViews = new RemoteViews(context.getPackageName(), R.layout.download_layout);
    notificationData.remoteViews.setTextViewText(R.id.filename, fileName + " 下载进度:");
    notificationData.remoteViews.setProgressBar(R.id.download_progess, 100, 0, false);
    NotificationCompat notificationCompat;
    Intent intent = new Intent();
    PendingIntent pendingIntent = null;
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, 0, cancelIntent, 0);
    notificationData.builder = new Notification.Builder(context);
    notificationData.builder.setContent(notificationData.remoteViews).setTicker("正在下载中").setContentIntent(pendingIntent).setDeleteIntent(pendingIntentCancel).setSmallIcon(R.drawable.down_load).setAutoCancel(false).setOngoing(true).setWhen(System.currentTimeMillis());
    Notification notification = notificationData.builder.build();
    notificationManager.notify(notificationData.notificationId, notification);
// context.startForeground(notificationData.notificationId, notification);
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Example 3 with NotificationCompat

use of android.support.v7.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());
}
Also used : NotificationManagerCompat(android.preview.support.v4.app.NotificationManagerCompat) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 4 with NotificationCompat

use of android.support.v7.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)
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) Date(java.util.Date)

Example 5 with NotificationCompat

use of android.support.v7.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)
}
Also used : NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Aggregations

PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 NotificationCompat (android.support.v4.app.NotificationCompat)5 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 RemoteViews (android.widget.RemoteViews)2 NotificationManagerCompat (android.preview.support.v4.app.NotificationManagerCompat)1 Date (java.util.Date)1