Example usage for android.support.v4.app NotificationManagerCompat notify

List of usage examples for android.support.v4.app NotificationManagerCompat notify

Introduction

In this page you can find the example usage for android.support.v4.app NotificationManagerCompat notify.

Prototype

public void notify(String str, int i, Notification notification) 

Source Link

Usage

From source file:com.android.messaging.sms.SmsStorageStatusManager.java

/**
 * Post sms storage low notification/* w w w. jav a  2  s .c o m*/
 */
private static void postStorageLowNotification() {
    final Context context = Factory.get().getApplicationContext();
    final Resources resources = context.getResources();
    final PendingIntent pendingIntent = UIIntents.get().getPendingIntentForLowStorageNotifications(context);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(resources.getString(R.string.sms_storage_low_title))
            .setTicker(resources.getString(R.string.sms_storage_low_notification_ticker))
            .setSmallIcon(R.drawable.ic_failed_light).setPriority(Notification.PRIORITY_DEFAULT)
            .setOngoing(true) // Can't be swiped off
            .setAutoCancel(false) // Don't auto cancel
            .setContentIntent(pendingIntent);

    final NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(builder);
    bigTextStyle.bigText(resources.getString(R.string.sms_storage_low_text));
    final Notification notification = bigTextStyle.build();

    final NotificationManagerCompat notificationManager = NotificationManagerCompat
            .from(Factory.get().getApplicationContext());

    notificationManager.notify(getNotificationTag(), PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID,
            notification);
}

From source file:com.android.messaging.receiver.SmsReceiver.java

public static void postNewMessageSecondaryUserNotification() {
    final Context context = Factory.get().getApplicationContext();
    final Resources resources = context.getResources();
    final PendingIntent pendingIntent = UIIntents.get()
            .getPendingIntentForSecondaryUserNewMessageNotification(context);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(resources.getString(R.string.secondary_user_new_message_title))
            .setTicker(resources.getString(R.string.secondary_user_new_message_ticker))
            .setSmallIcon(R.drawable.ic_sms_light)
            // Returning PRIORITY_HIGH causes L to put up a HUD notification. Without it, the ticker
            // isn't displayed.
            .setPriority(Notification.PRIORITY_HIGH).setContentIntent(pendingIntent);

    final NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(builder);
    bigTextStyle.bigText(resources.getString(R.string.secondary_user_new_message_title));
    final Notification notification = bigTextStyle.build();

    final NotificationManagerCompat notificationManager = NotificationManagerCompat
            .from(Factory.get().getApplicationContext());

    int defaults = Notification.DEFAULT_LIGHTS;
    if (BugleNotifications.shouldVibrate(new SecondaryUserNotificationState())) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }//from w ww .  ja  va 2 s . c om
    notification.defaults = defaults;

    notificationManager.notify(getNotificationTag(), PendingIntentConstants.SMS_SECONDARY_USER_NOTIFICATION_ID,
            notification);
}

From source file:com.oasisfeng.nevo.decorators.media.MediaPlayerDecorator.java

@Override
protected void apply(final StatusBarNotificationEvo evolving) throws Exception {
    if (evolving.isClearable())
        return; // Just sticky notification, to reduce the overhead.
    final INotification n = evolving.notification();
    RemoteViews content_view = n.getBigContentView(); // Prefer big content view since it usually contains more actions
    if (content_view == null)
        content_view = n.getContentView();
    if (content_view == null)
        return;// www. j  a  va  2 s.  c o m
    final AtomicReference<IntentSender> sender_holder = new AtomicReference<>();
    final View views = content_view.apply(new ContextWrapper(this) {
        @Override
        public void startIntentSender(final IntentSender intent, final Intent fillInIntent, final int flagsMask,
                final int flagsValues, final int extraFlags) throws IntentSender.SendIntentException {
            startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
        }

        @Override
        public void startIntentSender(final IntentSender intent, final Intent fillInIntent, final int flagsMask,
                final int flagsValues, final int extraFlags, final Bundle options)
                throws IntentSender.SendIntentException {
            sender_holder.set(intent);
        }
    }, null);
    if (!(views instanceof ViewGroup))
        return;

    final List<NotificationCompat.Action> actions = new ArrayList<>(8);
    findClickable((ViewGroup) views, new Predicate<View>() {
        @Override
        public boolean apply(final View v) {
            sender_holder.set(null);
            v.performClick(); // trigger startIntentSender() above
            final IntentSender sender = sender_holder.get();
            if (sender == null) {
                Log.w(TAG, v + " has OnClickListener but no PendingIntent in it.");
                return true;
            }

            final PendingIntent pending_intent = getPendingIntent(sender);
            if (v instanceof TextView) {
                final CharSequence text = ((TextView) v).getText();
                actions.add(new NotificationCompat.Action(0, text, pending_intent));
            } else if (v instanceof ImageView) {
                final int res = getImageViewResource((ImageView) v);
                CharSequence title = v.getContentDescription();
                if (title == null)
                    title = "<" + System.identityHashCode(v);
                actions.add(new NotificationCompat.Action(res, title, pending_intent));
            } else {
                CharSequence title = v.getContentDescription();
                if (title == null)
                    title = "<" + System.identityHashCode(v);
                actions.add(new NotificationCompat.Action(0, title, pending_intent));
            }
            return true;
        }
    });

    final Notification mirror;
    final String pkg = evolving.getPackageName();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final Notification.Builder b = new Notification.Builder(createPackageContext(pkg, 0))
                .setContentTitle(n.extras().getCharSequence(NotificationCompat.EXTRA_TITLE))
                .setLargeIcon(n.getLargeIcon()).setSmallIcon(fixIconPkg(n.getSmallIcon(), pkg));
        for (final NotificationCompat.Action action : actions)
            b.addAction(new Action.Builder(Icon.createWithResource(pkg, action.getIcon()), action.getTitle(),
                    action.getActionIntent()).build());
        mirror = b.build();
    } else {
        final NotificationCompat.Builder b = new NotificationCompat.Builder(createPackageContext(pkg, 0))
                .setContentTitle(n.extras().getCharSequence(NotificationCompat.EXTRA_TITLE))
                .setLargeIcon(n.extras().getParcelable(NotificationCompat.EXTRA_LARGE_ICON).<Bitmap>get())
                .setSmallIcon(android.R.drawable.ic_media_play);
        for (final NotificationCompat.Action action : actions)
            b.addAction(action);
        mirror = b.build();
    }
    final NotificationManagerCompat nm = NotificationManagerCompat.from(this);
    nm.notify("M<" + evolving.getPackageName() + (evolving.getTag() != null ? ">" + evolving.getTag() : ">"),
            evolving.getId(), mirror);
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

private static synchronized void doNotify(final Notification notification,
        final NotificationState notificationState) {
    if (notification == null) {
        return;//from w  w w .j  av a2  s . c  om
    }
    final int type = notificationState.mType;
    final ConversationIdSet conversationIds = notificationState.mConversationIds;
    final boolean isBundledNotification = (notificationState instanceof BundledMessageNotificationState);

    // Mark the notification as finished
    notificationState.mCanceled = true;

    final NotificationManagerCompat notificationManager = NotificationManagerCompat
            .from(Factory.get().getApplicationContext());
    // Only need conversationId for tags with a single conversation.
    String conversationId = null;
    if (conversationIds != null && conversationIds.size() == 1) {
        conversationId = conversationIds.first();
    }
    final String notificationTag = buildNotificationTag(type, conversationId, isBundledNotification);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    notificationManager.notify(notificationTag, type, notification);

    LogUtil.i(TAG, "Notifying for conversation " + conversationId + "; " + "tag = " + notificationTag
            + ", type = " + type);
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

/**
 * Check for failed messages and post notifications as needed.
 * TODO: Rewrite this as a NotificationState.
 *///from  ww w. j a v a  2  s  .c  om
public static void checkFailedMessages() {
    final DatabaseWrapper db = DataModel.get().getDatabase();

    final Cursor messageDataCursor = db.query(DatabaseHelper.MESSAGES_TABLE, MessageData.getProjection(),
            FailedMessageQuery.FAILED_MESSAGES_WHERE_CLAUSE, null /*selectionArgs*/, null /*groupBy*/,
            null /*having*/, FailedMessageQuery.FAILED_ORDER_BY);

    try {
        final Context context = Factory.get().getApplicationContext();
        final Resources resources = context.getResources();
        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        if (messageDataCursor != null) {
            final MessageData messageData = new MessageData();

            final HashSet<String> conversationsWithFailedMessages = new HashSet<String>();

            // track row ids in case we want to display something that requires this
            // information
            final ArrayList<Integer> failedMessages = new ArrayList<Integer>();

            int cursorPosition = -1;
            final long when = 0;

            messageDataCursor.moveToPosition(-1);
            while (messageDataCursor.moveToNext()) {
                messageData.bind(messageDataCursor);

                final String conversationId = messageData.getConversationId();
                if (DataModel.get().isNewMessageObservable(conversationId)) {
                    // Don't post a system notification for an observable conversation
                    // because we already show an angry red annotation in the conversation
                    // itself or in the conversation preview snippet.
                    continue;
                }

                cursorPosition = messageDataCursor.getPosition();
                failedMessages.add(cursorPosition);
                conversationsWithFailedMessages.add(conversationId);
            }

            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, "Found " + failedMessages.size() + " failed messages");
            }
            if (failedMessages.size() > 0) {
                final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

                CharSequence line1;
                CharSequence line2;
                final boolean isRichContent = false;
                ConversationIdSet conversationIds = null;
                PendingIntent destinationIntent;
                if (failedMessages.size() == 1) {
                    messageDataCursor.moveToPosition(cursorPosition);
                    messageData.bind(messageDataCursor);
                    final String conversationId = messageData.getConversationId();

                    // We have a single conversation, go directly to that conversation.
                    destinationIntent = UIIntents.get().getPendingIntentForConversationActivity(context,
                            conversationId, null /*draft*/);

                    conversationIds = ConversationIdSet.createSet(conversationId);

                    final String failedMessgeSnippet = messageData.getMessageText();
                    int failureStringId;
                    if (messageData.getStatus() == MessageData.BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED) {
                        failureStringId = R.string.notification_download_failures_line1_singular;
                    } else {
                        failureStringId = R.string.notification_send_failures_line1_singular;
                    }
                    line1 = resources.getString(failureStringId);
                    line2 = failedMessgeSnippet;
                    // Set rich text for non-SMS messages or MMS push notification messages
                    // which we generate locally with rich text
                    // TODO- fix this
                    //                        if (messageData.isMmsInd()) {
                    //                            isRichContent = true;
                    //                        }
                } else {
                    // We have notifications for multiple conversation, go to the conversation
                    // list.
                    destinationIntent = UIIntents.get().getPendingIntentForConversationListActivity(context);

                    int line1StringId;
                    int line2PluralsId;
                    if (messageData.getStatus() == MessageData.BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED) {
                        line1StringId = R.string.notification_download_failures_line1_plural;
                        line2PluralsId = R.plurals.notification_download_failures;
                    } else {
                        line1StringId = R.string.notification_send_failures_line1_plural;
                        line2PluralsId = R.plurals.notification_send_failures;
                    }
                    line1 = resources.getString(line1StringId);
                    line2 = resources.getQuantityString(line2PluralsId, conversationsWithFailedMessages.size(),
                            failedMessages.size(), conversationsWithFailedMessages.size());
                }
                line1 = applyWarningTextColor(context, line1);
                line2 = applyWarningTextColor(context, line2);

                final PendingIntent pendingIntentForDelete = UIIntents.get()
                        .getPendingIntentForClearingNotifications(context, BugleNotifications.UPDATE_ERRORS,
                                conversationIds, 0);

                builder.setContentTitle(line1).setTicker(line1)
                        .setWhen(when > 0 ? when : System.currentTimeMillis())
                        .setSmallIcon(R.drawable.ic_failed_light).setDeleteIntent(pendingIntentForDelete)
                        .setContentIntent(destinationIntent)
                        .setSound(UriUtil.getUriForResourceId(context, R.raw.message_failure));
                if (isRichContent && !TextUtils.isEmpty(line2)) {
                    final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);
                    if (line2 != null) {
                        inboxStyle.addLine(Html.fromHtml(line2.toString()));
                    }
                    builder.setStyle(inboxStyle);
                } else {
                    builder.setContentText(line2);
                }

                if (builder != null) {
                    notificationManager.notify(BugleNotifications
                            .buildNotificationTag(PendingIntentConstants.MSG_SEND_ERROR, null),
                            PendingIntentConstants.MSG_SEND_ERROR, builder.build());
                }
            } else {
                notificationManager.cancel(
                        BugleNotifications.buildNotificationTag(PendingIntentConstants.MSG_SEND_ERROR, null),
                        PendingIntentConstants.MSG_SEND_ERROR);
            }
        }
    } finally {
        if (messageDataCursor != null) {
            messageDataCursor.close();
        }
    }
}