Java Code Examples for android.support.v7.app.NotificationCompat#Builder

The following examples show how to use android.support.v7.app.NotificationCompat#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaNotificationManager.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private void setMediaStyleLayoutForNotificationBuilder(NotificationCompat.Builder builder) {
    setMediaStyleNotificationText(builder);
    if (!mMediaNotificationInfo.supportsPlayPause()) {
        builder.setLargeIcon(null);
    } else if (mMediaNotificationInfo.largeIcon != null) {
        builder.setLargeIcon(mMediaNotificationInfo.largeIcon);
    } else if (!isRunningN()) {
        if (mDefaultLargeIcon == null) {
            int resourceId = (mMediaNotificationInfo.defaultLargeIcon != 0)
                    ? mMediaNotificationInfo.defaultLargeIcon : R.drawable.audio_playing_square;
            mDefaultLargeIcon = scaleIconForDisplay(
                    BitmapFactory.decodeResource(mContext.getResources(), resourceId));
        }
        builder.setLargeIcon(mDefaultLargeIcon);
    }
    // TODO(zqzhang): It's weird that setShowWhen() don't work on K. Calling setWhen() to force
    // removing the time.
    builder.setShowWhen(false).setWhen(0);

    addNotificationButtons(builder);
}
 
Example 2
Source File: NotificationUtil.java    From Prodigal with Apache License 2.0 6 votes vote down vote up
public void showPlayingNotification(PlayerService service, MediaSessionCompat session) {
    NotificationCompat.Builder builder = notificationBuilder(service, session);
    if( builder == null ) {
        return;
    }
    NotificationCompat.Action action;
    if (session.isActive()) {
        action = new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", playIntent);
    } else {
        action = new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", pauseIntent);
    }
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, "Previous",
            prevIntent));
    builder.addAction(action);
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, "Next",
            nextIntent));

    builder.setOngoing(true);

    builder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(1).setMediaSession(session.getSessionToken()));
    builder.setSmallIcon(R.mipmap.ic_launcher);
    NotificationManagerCompat.from(service).notify(NOTIFICATION_ID, builder.build());
}
 
Example 3
Source File: MainActivity.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
public void sendNormal(View view) {
    //NOTIFICATION_SERVICE是Context的内容
    //1.获取NotificationManager
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //2.创建Notification对象
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    //设置小图标,必须设置SmallIcon和Ticker否则不弹出通知
    builder.setSmallIcon(R.mipmap.sing_icon)
            .setTicker("中国银行通知")
            .setContentText("工资----????")
            .setContentTitle("工资详情");

    //设置通知铃声……
    builder.setDefaults(Notification.DEFAULT_ALL);
    //创建对象
    Notification notification = builder.build();
    //3.通过manager发通知 
    manager.notify(100, notification);
}
 
Example 4
Source File: ChatNotificationManager.java    From TestChat with Apache License 2.0 5 votes vote down vote up
/**
 * 发送通知到通知栏
 *
 * @param isAllowVibrate 是否允许振动
 * @param isAllowVoice   是否允许声音
 * @param context        context
 * @param title          标题
 * @param icon           图标
 * @param content        内容
 * @param targetClass    目标Activity
 */
public void notify(String tag, String groupId, boolean isAllowVibrate, boolean isAllowVoice, Context context, String title, int icon, CharSequence content, Class<? extends Activity> targetClass) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(icon);
        builder.setContentText(content);
        builder.setContentTitle(title);
        builder.setTicker(title);
        builder.setAutoCancel(true);
        if (isAllowVibrate) {
                builder.setDefaults(Notification.DEFAULT_VIBRATE);
        }
        if (isAllowVoice) {
                builder.setDefaults(Notification.DEFAULT_SOUND);
        }
        LogUtil.e("设置通知123");
        if (targetClass!=null) {
                Intent intent = new Intent(context, targetClass);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra(Constant.NOTIFICATION_TAG, tag);
                if (groupId != null) {
                        intent.putExtra("groupId", groupId);
                }
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);
        }
        sNotificationManager.notify(Constant.NOTIFY_ID, builder.build());
        sNotificationManager.notify(Constant.NOTIFY_ID, builder.build());
}
 
Example 5
Source File: OfflineNotifier.java    From TLint with Apache License 2.0 5 votes vote down vote up
public void notifyThreads(Forum forum, long offlineLength) {
    String title = String.format("正在离线板块[%s]", forum.getName());
    String content = String.format("节省流量%s", FormatUtil.formatFileSize(offlineLength));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setContentText(content);

    notify(OfflineThreads, 0, builder);
}
 
Example 6
Source File: MediaNotificationManager.java    From react-native-streaming-audio-player with MIT License 5 votes vote down vote up
private void fetchBitmapFromURLAsync(final String bitmapUrl, final NotificationCompat.Builder builder) {
    AlbumArtCache.getInstance().fetch(bitmapUrl, new AlbumArtCache.FetchListener() {
        @Override
        public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
            if (mMetadata != null && mMetadata.getDescription().getIconUri() != null &&
                        mMetadata.getDescription().getIconUri().toString().equals(artUrl)) {
                // If the media is still the same, update the notification:
                builder.setLargeIcon(bitmap);
                mNotificationManager.notify(NOTIFICATION_ID, builder.build());
            }
        }
    });
}
 
Example 7
Source File: ActivityRecognizedService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private void raise_vehicle_notification(String msg) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentText(msg);
    builder.setSmallIcon(R.drawable.ic_launcher);
    if (VehicleMode.shouldPlaySound()) {
        setInternalPrefsLong(VEHICLE_MODE_LAST_ALERT, JoH.tsl());
        builder.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.labbed_musical_chime));
    }
    builder.setContentTitle(getString(R.string.app_name) + " " + "Vehicle mode");
    cancel_vehicle_notification();
    NotificationManagerCompat.from(this).notify(VEHICLE_NOTIFICATION_ID, builder.build());
}
 
Example 8
Source File: TickService.java    From Tick with MIT License 5 votes vote down vote up
@Override
public void onCountDownTick(long millisUntilFinished) {
    mApplication.setMillisUntilFinished(millisUntilFinished);

    Intent intent = new Intent(ACTION_COUNTDOWN_TIMER);
    intent.putExtra(MILLIS_UNTIL_FINISHED, millisUntilFinished);
    intent.putExtra(REQUEST_ACTION, ACTION_TICK);
    sendBroadcast(intent);

    NotificationCompat.Builder builder =
            getNotification(getNotificationTitle(), formatTime(millisUntilFinished));

    getNotificationManager().notify(NOTIFICATION_ID, builder.build());
}
 
Example 9
Source File: UpdateAgent.java    From update with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart() {
    if (mBuilder == null) {
        String title = "下载中 - " + mContext.getString(mContext.getApplicationInfo().labelRes);
        mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setOngoing(true)
                .setAutoCancel(false)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setSmallIcon(mContext.getApplicationInfo().icon)
                .setTicker(title)
                .setContentTitle(title);
    }
    onProgress(0);
}
 
Example 10
Source File: InsulinIntegrationNotify.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public NotificationCompat.Builder getErrorNotification(){

        if (foundError) {
            Context c = MainApp.instance();
            String title = "Error: Insulin Actions";
            String msg = errorMsg;
            Bitmap bitmap = Bitmap.createBitmap(320,320, Bitmap.Config.ARGB_8888);
            bitmap.eraseColor(Color.RED);

            Intent intent_open_activity = new Intent(c, MainActivity.class);
            PendingIntent pending_intent_open_activity = PendingIntent.getActivity(c, 2, intent_open_activity, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c);
            notificationBuilder.setSmallIcon(R.drawable.alert_circle);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(msg);
            notificationBuilder.setContentIntent(pending_intent_open_activity);
            notificationBuilder.setPriority(Notification.PRIORITY_MAX);
            notificationBuilder.setCategory(Notification.CATEGORY_ALARM);
            notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            notificationBuilder.setVibrate(new long[]{500, 1000, 500, 500, 500, 1000, 500});

            return notificationBuilder;

        } else {
            return null;
        }
    }
 
Example 11
Source File: MediaNotificationManager.java    From delion with Apache License 2.0 5 votes vote down vote up
private void setMediaStyleNotificationText(NotificationCompat.Builder builder) {
    builder.setContentTitle(mMediaNotificationInfo.metadata.getTitle());
    String artistAndAlbumText = getArtistAndAlbumText(mMediaNotificationInfo.metadata);
    if (isRunningN() || !artistAndAlbumText.isEmpty()) {
        builder.setContentText(artistAndAlbumText);
        builder.setSubText(mMediaNotificationInfo.origin);
    } else {
        // Leaving ContentText empty looks bad, so move origin up to the ContentText.
        builder.setContentText(mMediaNotificationInfo.origin);
    }
}
 
Example 12
Source File: TermService.java    From Ansole with GNU General Public License v2.0 5 votes vote down vote up
private Notification createNotification(PendingIntent pendingIntent) {
       NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
       builder.setOngoing(true);
       builder.setContentIntent(pendingIntent);
       builder.setSmallIcon(R.drawable.ic_stat_service_notification_icon);
       builder.setWhen(System.currentTimeMillis());
       builder.setTicker(getText(R.string.service_notify_text));
       builder.setContentTitle(getText(R.string.application_terminal));
       builder.setContentText(getText(R.string.service_notify_text));
       return builder.build();
}
 
Example 13
Source File: dex_smali.java    From styT with Apache License 2.0 5 votes vote down vote up
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    SharedPreferences sharedPreferences = getSharedPreferences("nico.styTool_preferences", MODE_PRIVATE);
    boolean isFirstRun = sharedPreferences.getBoolean("ok_c", true);
    //Editor editor = sharedPreferences.edit();
    if (isFirstRun) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("妮哩");
        builder.setContentText("QQ抢红包正在运行");
        builder.setOngoing(true);
        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NOTIFICATION_ID, notification);
    } else {

    }

    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        List<CharSequence> texts = event.getText();
        if (!texts.isEmpty()) {
            for (CharSequence text : texts) {
                String content = text.toString();
                if (content.contains(QQ_KEYWORD_NOTIFICATION)) {
                    openNotify(event);
                    return;
                }
            }
        }
    }
    openHongBao(event);
}
 
Example 14
Source File: Notifications.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void debugCard(Context c, APSResult apsResult){

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        if (prefs.getBoolean("debug_notification", false)) {

            String title, msg="";
            Date timeNow = new Date();
            DateFormat df = new SimpleDateFormat("HH:mm:ss c");

            title = "Last run: " + df.format(timeNow);
            if (apsResult != null){
                msg = "Eventual:" +  apsResult.getEventualBG() + " Snooze:" +  apsResult.getSnoozeBG() + " Temp?:" + apsResult.getTempSuggested();
            }

            Intent intent_open_activity = new Intent(c,MainActivity.class);
            PendingIntent pending_intent_open_activity = PendingIntent.getActivity(c, 4, intent_open_activity, PendingIntent.FLAG_UPDATE_CURRENT);

            Bitmap bitmap = Bitmap.createBitmap(320, 320, Bitmap.Config.ARGB_8888);
            bitmap.eraseColor(c.getResources().getColor(R.color.secondary_text_light));

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setColor(c.getResources().getColor(R.color.primary));
            notificationBuilder.extend(new NotificationCompat.WearableExtender().setBackground(bitmap));
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(msg);
            notificationBuilder.setContentIntent(pending_intent_open_activity);
            notificationBuilder.setPriority(Notification.PRIORITY_DEFAULT);
            notificationBuilder.setCategory(Notification.CATEGORY_STATUS);
            notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainApp.instance());
            notificationManager.notify(DEBUG_CARD, notificationBuilder.build());
        }
    }
 
Example 15
Source File: NotifyManager.java    From LLApp with Apache License 2.0 4 votes vote down vote up
/**
 * 显示一个带进度条的通知
 * @param context
 */
public static void showNotifyProgress(Context context,int flag){
    //进度条通知
    final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);
    builderProgress.setContentTitle("下载中");
    builderProgress.setSmallIcon(R.mipmap.ic_launcher);
    builderProgress.setTicker("进度条通知");
    builderProgress.setProgress(100, 0, false);
    final Notification notification = builderProgress.build();
    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    //发送一个通知
    notificationManager.notify(flag, notification);
    /**创建一个计时器,模拟下载进度**/
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        int progress = 0;

        @Override
        public void run() {
            Log.i("progress", progress + "");
            while (progress <= 100) {
                progress++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //更新进度条
                builderProgress.setProgress(100, progress, false);
                //再次通知
                notificationManager.notify(2, builderProgress.build());
            }
            //计时器退出
            this.cancel();
            //进度条退出
            notificationManager.cancel(2);
            return;//结束方法
        }
    }, 0);
}
 
Example 16
Source File: MediaNotificationManager.java    From delion with Apache License 2.0 4 votes vote down vote up
private void updateNotification() {
    if (mService == null) return;

    if (mMediaNotificationInfo == null) return;

    updateMediaSession();

    mNotificationBuilder = new NotificationCompat.Builder(mContext);
    if (ChromeFeatureList.isEnabled(ChromeFeatureList.MEDIA_STYLE_NOTIFICATION)) {
        setMediaStyleLayoutForNotificationBuilder(mNotificationBuilder);
    } else {
        setCustomLayoutForNotificationBuilder(mNotificationBuilder);
    }
    mNotificationBuilder.setSmallIcon(mMediaNotificationInfo.icon);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setLocalOnly(true);

    if (mMediaNotificationInfo.supportsSwipeAway()) {
        mNotificationBuilder.setOngoing(!mMediaNotificationInfo.isPaused);
    }

    // The intent will currently only be null when using a custom tab.
    // TODO(avayvod) work out what we should do in this case. See https://crbug.com/585395.
    if (mMediaNotificationInfo.contentIntent != null) {
        mNotificationBuilder.setContentIntent(PendingIntent.getActivity(mContext,
                mMediaNotificationInfo.tabId, mMediaNotificationInfo.contentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT));
        // Set FLAG_UPDATE_CURRENT so that the intent extras is updated, otherwise the
        // intent extras will stay the same for the same tab.
    }

    mNotificationBuilder.setVisibility(
            mMediaNotificationInfo.isPrivate ? NotificationCompat.VISIBILITY_PRIVATE
                                             : NotificationCompat.VISIBILITY_PUBLIC);

    Notification notification = mNotificationBuilder.build();

    // We keep the service as a foreground service while the media is playing. When it is not,
    // the service isn't stopped but is no longer in foreground, thus at a lower priority.
    // While the service is in foreground, the associated notification can't be swipped away.
    // Moving it back to background allows the user to remove the notification.
    if (mMediaNotificationInfo.supportsSwipeAway() && mMediaNotificationInfo.isPaused) {
        mService.stopForeground(false /* removeNotification */);

        NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
        manager.notify(mMediaNotificationInfo.id, notification);
    } else {
        mService.startForeground(mMediaNotificationInfo.id, notification);
    }
}
 
Example 17
Source File: PlayManager.java    From BeMusic with Apache License 2.0 4 votes vote down vote up
private void notificationLollipop (@PlayService.State int state) {
    NotificationManagerCompat notifyManager = NotificationManagerCompat.from(mContext);
    NotificationCompat.Builder builder = mNotifyAgent.getBuilder(mContext, this, state, mSong);
    notifyManager.notify(1, builder.build());
}
 
Example 18
Source File: NotificationManager.java    From Cheerleader with Apache License 2.0 4 votes vote down vote up
/**
 * Init all static components of the notification.
 *
 * @param context context used to instantiate the builder.
 */
private void initNotificationBuilder(Context context) {

    // inti builder.
    mNotificationBuilder = new NotificationCompat.Builder(context);
    mNotificationView = new RemoteViews(context.getPackageName(),
            R.layout.simple_sound_cloud_notification);
    mNotificationExpandedView = new RemoteViews(context.getPackageName(),
            R.layout.simple_sound_cloud_notification_expanded);

    // add right icon on Lollipop.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        addSmallIcon(mNotificationView);
        addSmallIcon(mNotificationExpandedView);
    }

    // set pending intents
    mNotificationView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_previous, mPreviousPendingIntent);
    mNotificationExpandedView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_previous, mPreviousPendingIntent);
    mNotificationView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_next, mNextPendingIntent);
    mNotificationExpandedView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_next, mNextPendingIntent);
    mNotificationView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_play, mTogglePlaybackPendingIntent);
    mNotificationExpandedView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_play, mTogglePlaybackPendingIntent);
    mNotificationView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_clear, mClearPendingIntent);
    mNotificationExpandedView.setOnClickPendingIntent(
            R.id.simple_sound_cloud_notification_clear, mClearPendingIntent);

    // add icon for action bar.
    mNotificationBuilder.setSmallIcon(mNotificationConfig.getNotificationIcon());

    // set the remote view.
    mNotificationBuilder.setContent(mNotificationView);

    // set the notification priority.
    mNotificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

    mNotificationBuilder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());

    // set the content intent.
    Class<?> playerActivity = mNotificationConfig.getNotificationActivity();
    if (playerActivity != null) {
        Intent i = new Intent(context, playerActivity);
        PendingIntent contentIntent = PendingIntent.getActivity(context, REQUEST_DISPLAYING_CONTROLLER,
                i, PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationBuilder.setContentIntent(contentIntent);
    }
}
 
Example 19
Source File: NotificationAgent.java    From BeMusic with Apache License 2.0 2 votes vote down vote up
/**
 * custom your notification style
 * @param context
 * @param manager
 * @param state
 * @param song
 * @return
 */
NotificationCompat.Builder getBuilder (Context context, PlayManager manager, @PlayService.State int state, Song song);
 
Example 20
Source File: NotificationUtils.java    From JkStepSensor with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @author leibing
 * @createTime 2016/09/02
 * @lastModify 2016/09/02
 * @param context 上下文
 * @return
 */
private NotificationUtils(Context context){
    builder = new NotificationCompat.Builder(context);
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}