android.app.NotificationChannel Java Examples

The following examples show how to use android.app.NotificationChannel. 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: NotificationChannels.java    From mollyim-android with GNU General Public License v3.0 7 votes vote down vote up
@TargetApi(26)
private static @NonNull NotificationChannel copyChannel(@NonNull NotificationChannel original, @NonNull String id) {
  NotificationChannel copy = new NotificationChannel(id, original.getName(), original.getImportance());

  copy.setGroup(original.getGroup());
  copy.setSound(original.getSound(), original.getAudioAttributes());
  copy.setBypassDnd(original.canBypassDnd());
  copy.enableVibration(original.shouldVibrate());
  copy.setVibrationPattern(original.getVibrationPattern());
  copy.setLockscreenVisibility(original.getLockscreenVisibility());
  copy.setShowBadge(original.canShowBadge());
  copy.setLightColor(original.getLightColor());
  copy.enableLights(original.shouldShowLights());

  return copy;
}
 
Example #2
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
void lockFieldsForUpdate(NotificationChannel original, NotificationChannel update) {
    if (original.canBypassDnd() != update.canBypassDnd()) {
        update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
    }
    if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) {
        update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
    }
    if (original.getImportance() != update.getImportance()) {
        update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
    }
    if (original.shouldShowLights() != update.shouldShowLights()
            || original.getLightColor() != update.getLightColor()) {
        update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
    }
    if (!Objects.equals(original.getSound(), update.getSound())) {
        update.lockFields(NotificationChannel.USER_LOCKED_SOUND);
    }
    if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern())
            || original.shouldVibrate() != update.shouldVibrate()) {
        update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
    }
    if (original.canShowBadge() != update.canShowBadge()) {
        update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
    }
}
 
Example #3
Source File: MainActivity.java    From BroadCastReceiver with Apache License 2.0 6 votes vote down vote up
/**
 * for API 26+ create notification channels
 */
private void createchannel() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = new NotificationChannel(id,
            getString(R.string.channel_name),  //name of the channel
            NotificationManager.IMPORTANCE_DEFAULT);   //importance level
        //important level: default is is high on the phone.  high is urgent on the phone.  low is medium, so none is low?
        // Configure the notification channel.
        mChannel.setDescription(getString(R.string.channel_description));
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setShowBadge(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        nm.createNotificationChannel(mChannel);

    }
}
 
Example #4
Source File: TriggerTasksService.java    From FreezeYou with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) || new AppPreferences(getApplicationContext()).getBoolean("useForegroundService", false)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder mBuilder = new Notification.Builder(this);
            mBuilder.setSmallIcon(R.drawable.ic_notification);
            mBuilder.setContentText(getString(R.string.backgroundService));
            NotificationChannel channel = new NotificationChannel("BackgroundService", getString(R.string.backgroundService), NotificationManager.IMPORTANCE_NONE);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null)
                notificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId("BackgroundService");
            Intent resultIntent = new Intent(getApplicationContext(), Main.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            startForeground(1, mBuilder.build());
        } else {
            startForeground(1, new Notification());
        }
    }
}
 
Example #5
Source File: MainActivity.java    From splitapkinstall with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createNotificationChannels(Context appCtx) {
    try {
        final NotificationManager notificationManager = (NotificationManager) appCtx.getSystemService(Context.NOTIFICATION_SERVICE);
        // Notification with sound channel
        CharSequence name = appCtx.getString(R.string.export_notification);
        String description = appCtx.getString(R.string.export_notification_description);
        NotificationChannel channel = new NotificationChannel("export_sound", name, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(description);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationManager.createNotificationChannel(channel);
    } catch (Exception e)
    {
        e.printStackTrace();
    }

}
 
Example #6
Source File: ShadowsocksNotification.java    From ShadowsocksRR with Apache License 2.0 6 votes vote down vote up
private void initNotificationBuilder() {
    String channelId = "net_speed";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, "NetSpeed", NotificationManager.IMPORTANCE_DEFAULT);
        nm.createNotificationChannel(channel);
    }
    builder = new NotificationCompat.Builder(service, channelId)
            .setWhen(0)
            .setColor(ContextCompat.getColor(service, R.color.material_accent_500))
            .setTicker(service.getString(R.string.forward_success))
            .setContentTitle(profileName)
            .setContentIntent(PendingIntent.getActivity(service, 0, new Intent(service, Shadowsocks.class)
                    .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0))
            .setSmallIcon(R.drawable.ic_stat_shadowsocks);
    builder.addAction(R.drawable.ic_navigation_close,
            service.getString(R.string.stop),
            PendingIntent.getBroadcast(service, 0, new Intent(Constants.Action.CLOSE), 0));

    List<Profile> profiles = app.profileManager.getAllProfiles();
    if (profiles != null && !profiles.isEmpty()) {
        builder.addAction(R.drawable.ic_action_settings, service.getString(R.string.quick_switch),
                PendingIntent.getActivity(service, 0, new Intent(Constants.Action.QUICK_SWITCH), 0));
    }
}
 
Example #7
Source File: PlayerNotification.java    From blade-player with GNU General Public License v3.0 6 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel()
{
    NotificationManager mNotificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
    // The id of the channel.
    // The user-visible name of the channel.
    CharSequence name = "Media playback";
    // The user-visible description of the channel.
    String description = "Media playback controls";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    // Configure the notification channel.
    mChannel.setDescription(description);
    mChannel.setShowBadge(false);
    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    mNotificationManager.createNotificationChannel(mChannel);
}
 
Example #8
Source File: NotificationService.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private void createNotificationChannel()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        AudioAttributes attr = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        CharSequence name = context.getString(R.string.app_name);
        String description = context.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        channel.setSound(notification, attr);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example #9
Source File: NotificationHelper.java    From android-NotificationChannels with Apache License 2.0 6 votes vote down vote up
/**
 * Registers notification channels, which can be used later by individual notifications.
 *
 * @param ctx The application context
 */
public NotificationHelper(Context ctx) {
    super(ctx);

    NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
            getString(R.string.noti_channel_default), NotificationManager.IMPORTANCE_DEFAULT);
    chan1.setLightColor(Color.GREEN);
    chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    getManager().createNotificationChannel(chan1);

    NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
            getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
    chan2.setLightColor(Color.BLUE);
    chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    getManager().createNotificationChannel(chan2);
}
 
Example #10
Source File: NotificationChannels.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(26)
public static boolean isSoundDifferent(String id, NotificationChannel x) {
    if (x.getSound() == null) return false; // this does not have a sound
    final NotificationChannel c = getNotifManager().getNotificationChannel(id);
    if (c == null) return false; // no channel with this id
    if (c.getSound() == null)
        return false; // this maybe will only happen if user disables sound so lets not create a new one in that case

    final String original_sound = PersistentStore.getString("original-channel-sound-" + id);
    if (original_sound.equals("")) {
        PersistentStore.setString("original-channel-sound-" + id, x.getSound().toString());
        return false; // no existing record so save the original and do nothing else
    }
    if (original_sound.equals(x.getSound().toString()))
        return false; // its the same sound still
    return true; // the sound has changed vs the original
}
 
Example #11
Source File: NotificationObserver.java    From Kore with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void buildNotificationChannel() {

    NotificationChannel channel =
            new NotificationChannel(NOTIFICATION_CHANNEL,
                    service.getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_LOW);
    channel.enableLights(false);
    channel.enableVibration(false);
    channel.setShowBadge(false);

    NotificationManager notificationManager =
            (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager != null)
        notificationManager.createNotificationChannel(channel);
}
 
Example #12
Source File: NotificationChannels.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(26)
public static boolean isSoundDifferent(String id, NotificationChannel x) {
    if (x.getSound() == null) return false; // this does not have a sound
    final NotificationChannel c = getNotifManager().getNotificationChannel(id);
    if (c == null) return false; // no channel with this id
    if (c.getSound() == null)
        return false; // this maybe will only happen if user disables sound so lets not create a new one in that case

    final String original_sound = PersistentStore.getString("original-channel-sound-" + id);
    if (original_sound.equals("")) {
        PersistentStore.setString("original-channel-sound-" + id, x.getSound().toString());
        return false; // no existing record so save the original and do nothing else
    }
    if (original_sound.equals(x.getSound().toString()))
        return false; // its the same sound still
    return true; // the sound has changed vs the original
}
 
Example #13
Source File: ReservedValuesWorker.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void triggerNotification(String title, String content) {
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(rv_channel, "ReservedValuesSync", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(getApplicationContext(), rv_channel)
                    .setSmallIcon(R.drawable.ic_sync)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setAutoCancel(false)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);


    notificationManager.notify(ReservedValuesWorker.SYNC_RV_ID, notificationBuilder.build());
}
 
Example #14
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
        boolean fromUser) {
    Preconditions.checkNotNull(updatedChannel);
    Preconditions.checkNotNull(updatedChannel.getId());
    Record r = getOrCreateRecord(pkg, uid);
    if (r == null) {
        throw new IllegalArgumentException("Invalid package");
    }
    NotificationChannel channel = r.channels.get(updatedChannel.getId());
    if (channel == null || channel.isDeleted()) {
        throw new IllegalArgumentException("Channel does not exist");
    }
    if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
        updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
    }
    if (!fromUser) {
        updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
    }
    if (fromUser) {
        updatedChannel.lockFields(channel.getUserLockedFields());
        lockFieldsForUpdate(channel, updatedChannel);
    }
    r.channels.put(updatedChannel.getId(), updatedChannel);

    if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) {
        // copy settings to app level so they are inherited by new channels
        // when the app migrates
        r.importance = updatedChannel.getImportance();
        r.priority = updatedChannel.canBypassDnd()
                ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
        r.visibility = updatedChannel.getLockscreenVisibility();
        r.showBadge = updatedChannel.canShowBadge();
    }

    if (!channel.equals(updatedChannel)) {
        // only log if there are real changes
        MetricsLogger.action(getChannelLog(updatedChannel, pkg));
    }

    if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd) {
        updateChannelsBypassingDnd();
    }
    updateConfig();
}
 
Example #15
Source File: IntervalometerService.java    From timelapse-sony with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    mBroadcastManager = LocalBroadcastManager.getInstance(this);
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mCameraAPI = ((TimelapseApplication) getApplication()).getCameraAPI();

    mTimelapseData = ((TimelapseApplication) getApplication()).getTimelapseData();
    mApiRequestsList = mTimelapseData.getApiRequestsList();
    mStateMachineConnection = ((TimelapseApplication) getApplication()).
            getStateMachineConnection();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_MIN;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(false);
        mChannel.enableVibration(false);
        mNotificationManager.createNotificationChannel(mChannel);
    }

}
 
Example #16
Source File: IdenticonRemovalService.java    From Identiconizer with Apache License 2.0 6 votes vote down vote up
private Notification createNotification() {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
        NotificationChannel chan = new NotificationChannel(TAG, TAG, NotificationManager.IMPORTANCE_NONE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(chan);
    }
    Intent intent = new Intent(this, IdenticonsSettings.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    return new NotificationCompat.Builder(this, TAG)
            .setAutoCancel(false)
            .setOngoing(true)
            .setContentTitle(getString(R.string.identicons_remove_service_running_title))
            .setContentText(getString(R.string.identicons_remove_service_running_summary))
            .setSmallIcon(R.drawable.ic_settings_identicons)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent)
            .build();
}
 
Example #17
Source File: MediaNotificationManager.java    From android-MediaBrowserService with Apache License 2.0 6 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
    if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
        // The user-visible name of the channel.
        CharSequence name = "MediaSession";
        // The user-visible description of the channel.
        String description = "MediaSession and MediaPlayer";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(
                new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);
        Log.d(TAG, "createChannel: New channel created");
    } else {
        Log.d(TAG, "createChannel: Existing channel reused");
    }
}
 
Example #18
Source File: DownloadManager.java    From QuranAndroid with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Init notification channels for android 8.0 and higher
 */
private String createNotificationChannel(Context context) {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
        channel.setDescription(CHANNEL_DESCRIPTION);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    return CHANNEL_ID;
}
 
Example #19
Source File: ApplicationEx.java    From NetGuard with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannels() {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationChannel foreground = new NotificationChannel("foreground", getString(R.string.channel_foreground), NotificationManager.IMPORTANCE_MIN);
    foreground.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
    nm.createNotificationChannel(foreground);

    NotificationChannel notify = new NotificationChannel("notify", getString(R.string.channel_notify), NotificationManager.IMPORTANCE_DEFAULT);
    notify.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
    nm.createNotificationChannel(notify);

    NotificationChannel access = new NotificationChannel("access", getString(R.string.channel_access), NotificationManager.IMPORTANCE_DEFAULT);
    access.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
    nm.createNotificationChannel(access);
}
 
Example #20
Source File: ClementineService.java    From Android-Remote with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a notification that shows, that we got a keep alive timeout
 */
@SuppressLint("InlinedApi")
private void showKeepAliveDisconnectNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(
                App.notificationChannel, "Default",
                NotificationManager.IMPORTANCE_LOW
        );

        mNotificationManager.createNotificationChannel(notificationChannel);
    }

     Notification notification = new NotificationCompat.Builder(this, App.notificationChannel)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.notification_disconnect_keep_alive))
            .setAutoCancel(true)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(Utilities.getClementineRemotePendingIntent(this))
            .build();
    mNotificationManager
            .notify(ClementineMediaSessionNotification.NOTIFIFCATION_ID, notification);
}
 
Example #21
Source File: MainActivity.java    From habpanelviewer with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onPause() {
    unbindService(mSC);
    if (mService != null) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("de.vier_bier.habpanelviewer.status", "Status", NotificationManager.IMPORTANCE_MIN);
            channel.enableLights(false);
            channel.setSound(null, null);
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "de.vier_bier.habpanelviewer.status");
        builder.setSmallIcon(R.drawable.logo);
        mService.startForeground(42, builder.build());
    }

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    boolean pauseWebview = prefs.getBoolean(Constants.PREF_PAUSE_WEBVIEW, false);
    if (pauseWebview && !((PowerManager) getSystemService(POWER_SERVICE)).isInteractive()) {
        mWebView.pause();
    }
    super.onPause();
}
 
Example #22
Source File: RootExecService.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {

        NotificationChannel notificationChannel = new NotificationChannel
                (ROOT_CHANNEL_ID, getString(R.string.notification_channel_root), NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setDescription("");
        notificationChannel.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
        notificationChannel.enableLights(false);
        notificationChannel.enableVibration(false);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(notificationChannel);

        sendNotification(getString(R.string.app_name), getText(R.string.notification_temp_text).toString());

    }

    if (intent == null) {
        stopService(startId);
        return START_NOT_STICKY;
    }

    final String action = intent.getAction();

    if ((action == null) || (action.isEmpty())) {

        stopService(startId);
        return START_NOT_STICKY;
    }

    if (action.equals(RUN_COMMAND)) {
        RootCommands rootCommands = (RootCommands) intent.getSerializableExtra("Commands");
        int mark = intent.getIntExtra("Mark", 0);
        ExecRunnable execCommands = new ExecRunnable(rootCommands, mark, startId);
        executorService.execute(execCommands);
    }

    return START_NOT_STICKY;
}
 
Example #23
Source File: RequestFeedListDataService.java    From Focus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * android 8.0 新增的notification channel这里需要做一个判断
 *
 * @param context
 */
public void initChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("focus_pull_data",
            "Channel focus",
            NotificationManager.IMPORTANCE_MIN);
    channel.setDescription("更新订阅的数据");
    mNotificationManager.createNotificationChannel(channel);
}
 
Example #24
Source File: NotificationHelper.java    From azure-notificationhubs-android with Apache License 2.0 5 votes vote down vote up
public static void createChannelAndHandleNotifications(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                NOTIFICATION_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
        channel.setShowBadge(true);

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        NotificationsManager.handleNotifications(context, DemoNotificationsHandler.class);
    }
}
 
Example #25
Source File: NotificationChannelGroupCompat.java    From notification-channel-compat with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the list of channels that belong to this group
 */
public List<NotificationChannelCompat> getChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        List<NotificationChannel> originals = _notificationChannelGroup.getChannels();
        List<NotificationChannelCompat> channels = new ArrayList<>(originals.size());
        for (NotificationChannel origin : originals)
            channels.add(new NotificationChannelCompat(origin));
        return channels;
    }
    return mChannels;
}
 
Example #26
Source File: GoogleMainApplication.java    From traccar-manager-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    firebaseAnalytics = FirebaseAnalytics.getInstance(this);
    IntentFilter intentFilter = new IntentFilter(MainFragment.EVENT_LOGIN);
    LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                getString(R.string.notification_channel_id), getString(R.string.notification_channel),
                NotificationManager.IMPORTANCE_DEFAULT);
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).createNotificationChannel(channel);
    }
}
 
Example #27
Source File: WeChatDecorator.java    From decorator-wechat with Apache License 2.0 5 votes vote down vote up
@RequiresApi(O) private NotificationChannel makeChannel(final String channel_id, final @StringRes int name, final boolean silent) {
	final NotificationChannel channel = new NotificationChannel(channel_id, getString(name), NotificationManager.IMPORTANCE_HIGH/* Allow heads-up (by default) */);
	if (silent) channel.setSound(null, null);
	else channel.setSound(getDefaultSound(), new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
			.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build());
	channel.enableLights(true);
	channel.setLightColor(LIGHT_COLOR);
	return channel;
}
 
Example #28
Source File: TransferNotificationManager.java    From nitroshare-android with MIT License 5 votes vote down vote up
/**
 * Create and register a notification channel
 * @param channelId unique ID for the channel
 * @param nameResId string resource for the channel name
 * @param importance notification priority
 * @param flash true to enable lights and vibration
 */
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel(String channelId, @StringRes int nameResId, int importance, boolean flash) {
    NotificationChannel channel = new NotificationChannel(channelId,
            mService.getString(nameResId), importance);
    if (flash) {
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setShowBadge(true);
    }
    mNotificationManager.createNotificationChannel(channel);
}
 
Example #29
Source File: AlarmHandler.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
public void showAlarmNotification(Context context)
{
    AlarmEntryReader reader = AlarmEntryReader.construct(context);
    String notifyText = reader.getNotificationText();

    Intent notifyIntent = new Intent(context, MainActivity.class);

    PendingIntent notifyPendingIntent =
            PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "openScale_notify");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(
                "openScale_notify",
                "openScale weight notification",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    Notification notification = mBuilder.setSmallIcon(R.drawable.ic_launcher_openscale)
                                        .setContentTitle(context.getString(R.string.app_name))
                                        .setContentText(notifyText)
                                        .setAutoCancel(true)
                                        .setContentIntent(notifyPendingIntent)
                                        .build();

    NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(ALARM_NOTIFICATION_ID, notification);
}
 
Example #30
Source File: KaliumMessagingService.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void initChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
            getString(R.string.app_name),
            NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Natrium transaction alerts");
    notificationManager.createNotificationChannel(channel);
}