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

The following examples show how to use android.support.v4.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: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static void showNotification(String title, String content, PendingIntent intent, int notificationId, String channelId, boolean sound, boolean vibrate, PendingIntent deleteIntent, Uri sound_uri, String bigmsg) {
    final NotificationCompat.Builder mBuilder = notificationBuilder(title, content, intent, channelId);
    final long[] vibratePattern = {0, 1000, 300, 1000, 300, 1000};
    if (vibrate) mBuilder.setVibrate(vibratePattern);
    if (deleteIntent != null) mBuilder.setDeleteIntent(deleteIntent);
    mBuilder.setLights(0xff00ff00, 300, 1000);
    if (sound) {
        Uri soundUri = (sound_uri != null) ? sound_uri : RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(soundUri);
    }

    if (bigmsg != null) {
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigmsg));
    }

    final NotificationManager mNotifyMgr = (NotificationManager) xdrip.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
    // if (!onetime) mNotifyMgr.cancel(notificationId);

    mNotifyMgr.notify(notificationId, XdripNotificationCompat.build(mBuilder));
}
 
Example 2
Source File: AlarmNotificationManager.java    From X-Alarm with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Notification createAlarmNotification(Context context, UUID alarmId) {

        Intent showUnlockIntent = new Intent(context, AlarmAlertFullScreen.class);
        showUnlockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        showUnlockIntent.putExtra(AlarmRingingService.ALARM_ID, alarmId);

        PendingIntent contentIntent = PendingIntent.getActivity(
                context,
                (int) Math.abs(alarmId.getLeastSignificantBits()),
                showUnlockIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("X-Alarm!")
                .setContentText(context.getString(R.string.default_label))
                .setOngoing(true) // Can not be dismissed
                .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                .setContentIntent(contentIntent);

        return builder.build();
    }
 
Example 3
Source File: ToDayStateActivity.java    From ToDay with MIT License 6 votes vote down vote up
/**
 * Generates a notification with the pending intent to send the user to the Flow State at the current task
 * being completed
 *
 * @return
 */
private NotificationCompat.Builder buildNotification() {
    Intent notificationIntent = new Intent(getApplicationContext(), ToDayStateActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
            .putExtra(AppConstants.EXTRA_PASSING_UUID, parentFlow.getUuid());
    PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.today_color_success)
                    .setColor(getResources().getColor(R.color.colorPrimary))
                    .setContentIntent(intent)
                    .setContentTitle(getString(R.string.fs_notification_title))
                    .setContentText("In Flow State")
                    .setAutoCancel(false)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    // Gets an instance of the NotificationManager service

    return builder;
}
 
Example 4
Source File: NotificationHelper.java    From FastAccess with GNU General Public License v3.0 6 votes vote down vote up
public static void collapseFAService(Context context, int size) {
    context.stopService(new Intent(context, FloatingService.class));
    Intent notificationIntent = new Intent(context, FloatingService.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    int icon = R.drawable.ic_fa_notification;
    long finalTime = System.currentTimeMillis();
    if (PrefHelper.getBoolean(PrefConstant.STATUS_BAR_HIDDEN)) {
        icon = R.drawable.ic_notification;
    }
    notificationBuilder
            .setPriority(Notification.PRIORITY_LOW)
            .setWhen(finalTime)
            .setSmallIcon(icon)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(context.getString(R.string.click_to_start_service))
            .setNumber(size)
            .setAutoCancel(false)
            .setOngoing(true);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
 
Example 5
Source File: CompetitionNotificationHandler.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
private static void postNotificationWithBitmap(Context context, Bitmap bitmap, String title, String content, PendingIntent pendingIntent) {
  long when = System.currentTimeMillis();
  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.mipmap.hapramp_logo)
    .setContentTitle(title)
    .setContentText(content)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true)
    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setBigContentTitle(title))
    .setWhen(when)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    mBuilder.setSmallIcon(R.drawable.logo_white_72);
    mBuilder.setCategory(Notification.CATEGORY_SOCIAL);
    mBuilder.setColor(context.getResources().getColor(R.color.colorPrimary));
  } else {
    mBuilder.setSmallIcon(R.drawable.logo_white_24);
  }

  NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
  int notificationId = (int) System.currentTimeMillis();
  notificationManager.notify(notificationId, mBuilder.build());
}
 
Example 6
Source File: MyFirebaseMessagingService.java    From BusyBox with Apache License 2.0 6 votes vote down vote up
@Override public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> dataMap = remoteMessage.getData();

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_status_bar_icon)
        .setContentTitle(dataMap.get(TITLE_KEY))
        .setContentText(dataMap.get(MESSAGE_KEY))
        .setAutoCancel(true)
        .setSound(defaultSoundUri);

    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = MainActivity.linkIntent(this, dataMap.get(URI_KEY));

    PendingIntent pending = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT);

    notificationBuilder.setContentIntent(pending);

    notificationManager.notify(0, notificationBuilder.build());
}
 
Example 7
Source File: GoProNotificaionManager.java    From android-wear-GoPro-Remote with Apache License 2.0 6 votes vote down vote up
public void showPhotoNotificaion() {

        mNotificationManager.cancel(2);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(getContext())
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(getContext().getString(R.string.notification_title_photo))
                        .setContentText(getContext().getString(R.string.notification_content_photo))
                        .addAction(android.R.drawable.ic_menu_camera,
                                getContext().getString(R.string.gopro_action_take_photo),
                                getActionPendingIntent(GoProAction.TAKE_PHOTO))
                        .addAction(android.R.drawable.ic_menu_revert, getContext().getString(
                                        R.string.notification_back),
                                getShowDefaultNotificationPendingIntent()
                        )
                        .setDeleteIntent(getActionDismissedPendingIntent());

        Notification n1 = new WearableNotifications.Builder(notificationBuilder)
                .setGroup(GROUP_ID, 1)
                .build();
        mNotificationManager.notify(1, n1);
    }
 
Example 8
Source File: NotificationUtils.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
public static void remindUserBecauseCharging(Context context) {
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                WATER_REMINDER_NOTIFICATION_CHANNEL_ID,
                context.getString(R.string.main_notification_channel_name),
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
        }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,WATER_REMINDER_NOTIFICATION_CHANNEL_ID)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(R.drawable.ic_drink_notification)
            .setLargeIcon(largeIcon(context))
            .setContentTitle(context.getString(R.string.charging_reminder_notification_title))
            .setContentText(context.getString(R.string.charging_reminder_notification_body))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(
                    context.getString(R.string.charging_reminder_notification_body)))
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(contentIntent(context))
            .addAction(drinkWaterAction(context))
            .addAction(ignoreReminderAction(context))
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
    }
    notificationManager.notify(WATER_REMINDER_NOTIFICATION_ID, notificationBuilder.build());
}
 
Example 9
Source File: EarthquakeUpdateJobService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void broadcastNotification(Earthquake earthquake) {
  createNotificationChannel();

  Intent startActivityIntent = new Intent(this, EarthquakeMainActivity.class);
  PendingIntent launchIntent = PendingIntent.getActivity(this, 0,
    startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  final NotificationCompat.Builder earthquakeNotificationBuilder
    = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL);

  earthquakeNotificationBuilder
    .setSmallIcon(R.drawable.notification_icon)
    .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
    .setDefaults(NotificationCompat.DEFAULT_ALL)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setContentIntent(launchIntent)
    .setAutoCancel(true)
    .setShowWhen(true);

  earthquakeNotificationBuilder
    .setWhen(earthquake.getDate().getTime())
    .setContentTitle("M:" + earthquake.getMagnitude())
    .setContentText(earthquake.getDetails())
    .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(earthquake.getDetails()));

  NotificationManagerCompat notificationManager
    = NotificationManagerCompat.from(this);

  notificationManager.notify(NOTIFICATION_ID,
    earthquakeNotificationBuilder.build());
}
 
Example 10
Source File: ContactSync.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
private void newContactFound(int count){
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentTitle("Contact Sync!!");
    mBuilder.setContentText("Backup of your new "+count+" Contacts succesfully take.");
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // notificationID allows you to update the notification later on.
    mNotificationManager.notify(notificaitonId, mBuilder.build());
}
 
Example 11
Source File: Notifications.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private void calibrationNotificationCreate(String title, String content, Intent intent, int notificationId) {
    NotificationCompat.Builder mBuilder = notificationBuilder(title, content, intent);
    mBuilder.setVibrate(vibratePattern);
    mBuilder.setLights(0xff00ff00, 300, 1000);
    if(calibration_override_silent) {
        mBuilder.setSound(Uri.parse(calibration_notification_sound), AudioAttributes.USAGE_ALARM);
    } else {
        mBuilder.setSound(Uri.parse(calibration_notification_sound));
    }

    NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    //mNotifyMgr.cancel(notificationId);
    mNotifyMgr.notify(notificationId, mBuilder.build());
}
 
Example 12
Source File: AndroidNotifications.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
private android.app.Notification buildSingleMessageNotification(Drawable d, NotificationCompat.Builder builder, String sender, CharSequence text, Notification topNotification) {
    android.app.Notification notification = builder
            .setContentTitle(sender)
            .setContentText(text)
            .setLargeIcon(drawableToBitmap(d))
            .setContentIntent(PendingIntent.getActivity(context, 0,
                    Intents.openDialog(topNotification.getPeer(), false, context),
                    PendingIntent.FLAG_CANCEL_CURRENT))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
            .build();
    addCustomLedAndSound(topNotification, notification);
    return notification;
}
 
Example 13
Source File: TimingService.java    From Focus with GNU General Public License v3.0 5 votes vote down vote up
private Notification createNotice(String title, int progress){
        //消息管理
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        initChannels(getApplicationContext());
        builderProgress = new NotificationCompat.Builder(getApplicationContext(), "focus_pull_data");
        builderProgress.setContentTitle(title);
        builderProgress.setSmallIcon(R.mipmap.ic_focus_launcher_round);
//        builderProgress.setTicker("进度条通知");

        if (progress > 0){//全部获取完的时候不需要显示进度条了
            builderProgress.setContentText(progress + "%");
            builderProgress.setProgress(100, progress, false);
        }
        if (progress == 100){
            builderProgress.setContentText(title);
        }
        //绑定点击事件
        Intent intent = new Intent(UIUtil.getContext(), MainActivity.class);
        intent.putExtra(GlobalConfig.is_need_update_main,true);
        PendingIntent pending_intent_go = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builderProgress.setAutoCancel(true);
        builderProgress.setContentIntent(pending_intent_go);

        notification = builderProgress.build();

        return notification;
    }
 
Example 14
Source File: NotifyUtil.java    From NotificationDemo with Apache License 2.0 4 votes vote down vote up
public static NotificationCompat.Builder create(Context context, int largeIcon, int smallIcon, CharSequence title, CharSequence content) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), largeIcon);
    return new NotificationCompat.Builder(context).setShowWhen(true).setSmallIcon(smallIcon).setContentTitle(title).setContentText(content).setLargeIcon(bitmap);
}
 
Example 15
Source File: AlertPlayer.java    From NightWatch with GNU General Public License v3.0 4 votes vote down vote up
private void Vibrate(Context ctx, AlertType alert, String bgValue, Boolean overrideSilent, int timeFromStartPlaying) {
    Log.d(TAG, "Vibrate called timeFromStartPlaying = " + timeFromStartPlaying);
    Log.d("ALARM", "setting vibrate alarm");
    int profile = getAlertProfile(ctx);

    // We use timeFromStartPlaying as a way to force vibrating/ non vibrating...
    if (profile != ALERT_PROFILE_ASCENDING) {
        // We start from the non ascending part...
        timeFromStartPlaying = MAX_ASCENDING;
    }

    String title = bgValue + " " + alert.name;
    String content = "BG LEVEL ALERT: " + bgValue;
    Intent intent = new Intent(ctx, SnoozeActivity.class);

    NotificationCompat.Builder  builder = new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.ic_action_communication_invert_colors_on)
        .setContentTitle(title)
        .setContentText(content)
        .setContentIntent(notificationIntent(ctx, intent))
        .setDeleteIntent(snoozeIntent(ctx));
    if (profile != ALERT_PROFILE_VIBRATE_ONLY && profile != ALERT_PROFILE_SILENT) {
        if (timeFromStartPlaying >= MAX_VIBRATING) {
            // Before this, we only vibrate...
            float volumeFrac = (float)(timeFromStartPlaying - MAX_VIBRATING) / (MAX_ASCENDING - MAX_VIBRATING);
            volumeFrac = Math.min(volumeFrac, 1);
            if(profile == ALERT_PROFILE_MEDIUM) {
                volumeFrac = (float)0.7;
            }
            Log.d(TAG, "Vibrate volumeFrac = " + volumeFrac);
            boolean isRingTone = EditAlertActivity.isPathRingtone(ctx, alert.mp3_file);
            if(isRingTone && !overrideSilent) {
                    builder.setSound(Uri.parse(alert.mp3_file));
            } else {
                if(overrideSilent || isLoudPhone(ctx)) {
                    PlayFile(ctx, alert.mp3_file, volumeFrac);
                }
            }
        }
    }
    if (profile != ALERT_PROFILE_SILENT && alert.vibrate) {
        builder.setVibrate(Notifications.vibratePattern);
    }
    NotificationManager mNotifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.cancel(Notifications.exportAlertNotificationId);
    mNotifyMgr.notify(Notifications.exportAlertNotificationId, builder.build());
}
 
Example 16
Source File: NotificationUtils.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public static void createFtpNotification(Context context, Intent intent, int notification_id){
    RootInfo root = intent.getExtras().getParcelable(EXTRA_ROOT);
    if(null == root){
        return;
    }
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    long when = System.currentTimeMillis();

    CharSequence contentTitle = getString(context,R.string.ftp_notif_title);
    CharSequence contentText = String.format(getString(context,R.string.ftp_notif_text),
            ConnectionUtils.getFTPAddress(context));
    CharSequence tickerText = getString(context, R.string.ftp_notif_starting);
    CharSequence stopText = getString(context,R.string.ftp_notif_stop_server);

    Intent notificationIntent = new Intent(context, DocumentsActivity.class);
    notificationIntent.setData(root.getUri());
    notificationIntent.putExtras(intent.getExtras());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    Intent stopIntent = new Intent(ACTION_STOP_FTPSERVER);
    stopIntent.putExtras(intent.getExtras());
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0,
            stopIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_stat_server)
            .setTicker(tickerText)
            .setWhen(when)
            .setOngoing(true)
            .setColor(SettingsActivity.getPrimaryColor())
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setPriority(Notification.PRIORITY_MAX)
            .addAction(R.drawable.ic_action_stop, stopText, stopPendingIntent)
            .setShowWhen(false);

    Notification notification = builder.build();

    notificationManager.notify(notification_id, notification);
}
 
Example 17
Source File: MainActivity.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
public void sendGoto(View view) {
    //NOTIFICATION_SERVICE是Context的内容
    //1.获取NotificationManager
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //2.创建Notification对象
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    //设置小图标
    builder.setSmallIcon(R.mipmap.sing_icon)
            .setTicker("Title...")
            .setContentText("Content text....")
            .setContentTitle("Content title");

    //跳转到指定的Activity
    Intent intent = new Intent(this, MainActivity.class);
    //Intent-->PenddingIntent
    /**
     * 参数1: 上下文环境对象Context
     *
     * 参数2:Intent 的识别码
     *
     * 参数3:intent
     *
     * 参数4:Flag,表示当前的PendingIntent会把前面创建的PendingIntent更新
     */
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            1,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //builder关联PendigngIntent
    builder.setContentIntent(pendingIntent);

    //设置点击后,自动取消
    builder.setAutoCancel(true);

    //设置通知铃声……
    builder.setDefaults(Notification.DEFAULT_ALL);
    //创建对象
    Notification notification = builder.build();
    //3.通过manager发通知 
    manager.notify(101, notification);


}
 
Example 18
Source File: CallNotificationManager.java    From react-native-twilio-programmable-voice with MIT License 4 votes vote down vote up
public void createHangupLocalNotification(ReactApplicationContext context, String callSid, String caller) {
    PendingIntent pendingHangupIntent = PendingIntent.getBroadcast(
            context,
            0,
            new Intent(ACTION_HANGUP_CALL).putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID),
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    Intent launchIntent = new Intent(context, getMainActivityClass(context));
    launchIntent.setAction(ACTION_INCOMING_CALL)
            .putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    /*
     * Pass the notification id and call sid to use as an identifier to cancel the
     * notification later
     */
    Bundle extras = new Bundle();
    extras.putInt(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID);
    extras.putString(CALL_SID_KEY, callSid);
    extras.putString(NOTIFICATION_TYPE, ACTION_HANGUP_CALL);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, VOICE_CHANNEL)
            .setContentTitle("Call in progress")
            .setContentText(caller)
            .setSmallIcon(R.drawable.ic_call_white_24dp)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_CALL)
            .setOngoing(true)
            .setUsesChronometer(true)
            .setExtras(extras)
            .setContentIntent(activityPendingIntent);

    notification.addAction(0, "HANG UP", pendingHangupIntent);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // Create notifications channel (required for API > 25)
    initCallNotificationsChannel(notificationManager);
    notificationManager.notify(HANGUP_NOTIFICATION_ID, notification.build());
}
 
Example 19
Source File: NotificationUtil.java    From QuickLyric with GNU General Public License v3.0 4 votes vote down vote up
public static Notification makeNotification(Context context, String artist, String track, long duration, boolean retentionNotif, boolean isPlaying) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean prefOverlay = sharedPref.getBoolean("pref_overlay", false) && (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(context));
    int notificationPref = prefOverlay ? 2 : Integer.valueOf(sharedPref.getString("pref_notifications", "0"));

    Intent activityIntent = new Intent(context.getApplicationContext(), MainActivity.class)
            .setAction("com.geecko.QuickLyric.getLyrics")
            .putExtra("retentionNotif", retentionNotif)
            .putExtra("TAGS", new String[]{artist, track});
    Intent wearableIntent = new Intent("com.geecko.QuickLyric.SEND_TO_WEARABLE")
            .putExtra("artist", artist).putExtra("track", track).putExtra("duration", duration);
    final Intent overlayIntent = new Intent(context.getApplicationContext(), LyricsOverlayService.class)
            .setAction(LyricsOverlayService.CLICKED_FLOATING_ACTION);

    PendingIntent overlayPending = PendingIntent.getService(context.getApplicationContext(), 0, overlayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent openAppPending = PendingIntent.getActivity(context.getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    PendingIntent wearablePending = PendingIntent.getBroadcast(context.getApplicationContext(), 8, wearableIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Action wearableAction =
            new NotificationCompat.Action.Builder(R.drawable.ic_watch,
                    context.getString(R.string.wearable_prompt), wearablePending)
                    .build();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(notificationPref == 1 && isPlaying ? TRACK_NOTIF_CHANNEL : TRACK_NOTIF_HIDDEN_CHANNEL,
                context.getString(R.string.pref_notifications),
                notificationPref == 1 && isPlaying ? NotificationManager.IMPORTANCE_LOW : NotificationManager.IMPORTANCE_MIN);
        notificationChannel.setShowBadge(false);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context.getApplicationContext(), notificationPref == 1 && isPlaying ? TRACK_NOTIF_CHANNEL : TRACK_NOTIF_HIDDEN_CHANNEL);
    NotificationCompat.Builder wearableNotifBuilder = new NotificationCompat.Builder(context.getApplicationContext(), TRACK_NOTIF_HIDDEN_CHANNEL);

    int[] themes = new int[]{R.style.Theme_QuickLyric, R.style.Theme_QuickLyric_Red,
            R.style.Theme_QuickLyric_Purple, R.style.Theme_QuickLyric_Indigo,
            R.style.Theme_QuickLyric_Green, R.style.Theme_QuickLyric_Lime,
            R.style.Theme_QuickLyric_Brown, R.style.Theme_QuickLyric_Dark};
    int themeNum = Integer.valueOf(sharedPref.getString("pref_theme", "0"));

    context.setTheme(themes[themeNum]);

    notifBuilder.setSmallIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_notif : R.drawable.ic_notif4)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(String.format("%s - %s", artist, track))
            .setContentIntent(prefOverlay ? overlayPending : openAppPending)
            .setVisibility(-1) // Notification.VISIBILITY_SECRET
            .setGroup("Lyrics_Notification")
            .setColor(ColorUtils.getPrimaryColor(context))
            .setShowWhen(false)
            .setGroupSummary(true);

    wearableNotifBuilder.setSmallIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_notif : R.drawable.ic_notif4)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(String.format("%s - %s", artist, track))
            .setContentIntent(openAppPending)
            .setVisibility(-1) // Notification.VISIBILITY_SECRET
            .setGroup("Lyrics_Notification")
            .setOngoing(false)
            .setColor(ColorUtils.getPrimaryColor(context))
            .setGroupSummary(false)
            .setShowWhen(false)
            .extend(new NotificationCompat.WearableExtender().addAction(wearableAction));

    if (notificationPref == 2) {
        notifBuilder.setOngoing(true).setPriority(-2); // Notification.PRIORITY_MIN
        wearableNotifBuilder.setPriority(-2);
    } else
        notifBuilder.setPriority(-1); // Notification.PRIORITY_LOW

    Notification notif = notifBuilder.build();
    Notification wearableNotif = wearableNotifBuilder.build();

    if (notificationPref == 2 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        notif.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    if (notificationPref == 1)
        notif.flags |= Notification.FLAG_AUTO_CANCEL;

    try {
        context.getPackageManager().getPackageInfo("com.google.android.wearable.app", PackageManager.GET_META_DATA);
        NotificationManagerCompat.from(context).notify(8, wearableNotif); // TODO Make Android Wear app
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    return notif;
}
 
Example 20
Source File: Notification.java    From showCaseCordova with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor
 *
 * @param context
 *      Application context
 * @param options
 *      Parsed notification options
 * @param builder
 *      Pre-configured notification builder
 */
protected Notification (Context context, Options options,
                NotificationCompat.Builder builder, Class<?> receiver) {

    this.context = context;
    this.options = options;
    this.builder = builder;

    this.receiver = receiver != null ? receiver : defaultReceiver;
}