Search in sources :

Example 31 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Tusky by tuskyapp.

the class NotificationHelper method createNotificationChannelsForAccount.

public static void createNotificationChannelsForAccount(AccountEntity account, Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String[] channelIds = new String[] { CHANNEL_MENTION + account.getIdentifier(), CHANNEL_FOLLOW + account.getIdentifier(), CHANNEL_BOOST + account.getIdentifier(), CHANNEL_FAVOURITE + account.getIdentifier() };
        int[] channelNames = { R.string.notification_channel_mention_name, R.string.notification_channel_follow_name, R.string.notification_channel_boost_name, R.string.notification_channel_favourite_name };
        int[] channelDescriptions = { R.string.notification_channel_mention_descriptions, R.string.notification_channel_follow_description, R.string.notification_channel_boost_description, R.string.notification_channel_favourite_description };
        List<NotificationChannel> channels = new ArrayList<>(4);
        NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName());
        // noinspection ConstantConditions
        notificationManager.createNotificationChannelGroup(channelGroup);
        for (int i = 0; i < channelIds.length; i++) {
            String id = channelIds[i];
            String name = context.getString(channelNames[i]);
            String description = context.getString(channelDescriptions[i]);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(id, name, importance);
            channel.setDescription(description);
            channel.enableLights(true);
            channel.setLightColor(0xFF2B90D9);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channel.setGroup(account.getIdentifier());
            channels.add(channel);
        }
        // noinspection ConstantConditions
        notificationManager.createNotificationChannels(channels);
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList)

Example 32 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project android_packages_apps_Settings by crdroidandroid.

the class AppNotificationSettings method populateChannelList.

private void populateChannelList() {
    if (!mChannelGroups.isEmpty()) {
        // If there's anything in mChannelGroups, we've called populateChannelList twice.
        // Clear out existing channels and log.
        Log.w(TAG, "Notification channel group posted twice to settings - old size " + mChannelGroups.size() + ", new size " + mChannelGroupList.size());
        for (Preference p : mChannelGroups) {
            getPreferenceScreen().removePreference(p);
        }
    }
    if (mChannelGroupList.isEmpty()) {
        PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
        groupCategory.setTitle(R.string.notification_channels);
        groupCategory.setKey(KEY_GENERAL_CATEGORY);
        getPreferenceScreen().addPreference(groupCategory);
        mChannelGroups.add(groupCategory);
        Preference empty = new Preference(getPrefContext());
        empty.setTitle(R.string.no_channels);
        empty.setEnabled(false);
        groupCategory.addPreference(empty);
    } else {
        for (NotificationChannelGroup group : mChannelGroupList) {
            PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
            if (group.getId() == null) {
                groupCategory.setTitle(mChannelGroupList.size() > 1 ? R.string.notification_channels_other : R.string.notification_channels);
                groupCategory.setKey(KEY_GENERAL_CATEGORY);
            } else {
                groupCategory.setTitle(group.getName());
                groupCategory.setKey(group.getId());
            }
            groupCategory.setOrderingAsAdded(true);
            getPreferenceScreen().addPreference(groupCategory);
            mChannelGroups.add(groupCategory);
            final List<NotificationChannel> channels = group.getChannels();
            Collections.sort(channels, mChannelComparator);
            int N = channels.size();
            for (int i = 0; i < N; i++) {
                final NotificationChannel channel = channels.get(i);
                populateSingleChannelPrefs(groupCategory, channel);
            }
        }
        int deletedChannelCount = mBackend.getDeletedChannelCount(mAppRow.pkg, mAppRow.uid);
        if (deletedChannelCount > 0 && getPreferenceScreen().findPreference(KEY_DELETED) == null) {
            mDeletedChannels = new FooterPreference(getPrefContext());
            mDeletedChannels.setSelectable(false);
            mDeletedChannels.setTitle(getResources().getQuantityString(R.plurals.deleted_channels, deletedChannelCount, deletedChannelCount));
            mDeletedChannels.setEnabled(false);
            mDeletedChannels.setKey(KEY_DELETED);
            mDeletedChannels.setOrder(ORDER_LAST);
            getPreferenceScreen().addPreference(mDeletedChannels);
        }
    }
    updateDependents(mAppRow.banned);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) LayoutPreference(com.android.settings.applications.LayoutPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) MasterSwitchPreference(com.android.settings.widget.MasterSwitchPreference) FooterPreference(com.android.settingslib.widget.FooterPreference) Preference(android.support.v7.preference.Preference) PreferenceCategory(android.support.v7.preference.PreferenceCategory) FooterPreference(com.android.settingslib.widget.FooterPreference)

Example 33 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Conversations by siacs.

the class NotificationService method initializeChannels.

@RequiresApi(api = Build.VERSION_CODES.O)
void initializeChannels() {
    final Context c = mXmppConnectionService;
    final NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
    if (notificationManager == null) {
        return;
    }
    notificationManager.deleteNotificationChannel("export");
    notificationManager.deleteNotificationChannel("incoming_calls");
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("status", c.getString(R.string.notification_group_status_information)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats", c.getString(R.string.notification_group_messages)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("calls", c.getString(R.string.notification_group_calls)));
    final NotificationChannel foregroundServiceChannel = new NotificationChannel("foreground", c.getString(R.string.foreground_service_channel_name), NotificationManager.IMPORTANCE_MIN);
    foregroundServiceChannel.setDescription(c.getString(R.string.foreground_service_channel_description, c.getString(R.string.app_name)));
    foregroundServiceChannel.setShowBadge(false);
    foregroundServiceChannel.setGroup("status");
    notificationManager.createNotificationChannel(foregroundServiceChannel);
    final NotificationChannel errorChannel = new NotificationChannel("error", c.getString(R.string.error_channel_name), NotificationManager.IMPORTANCE_LOW);
    errorChannel.setDescription(c.getString(R.string.error_channel_description));
    errorChannel.setShowBadge(false);
    errorChannel.setGroup("status");
    notificationManager.createNotificationChannel(errorChannel);
    final NotificationChannel videoCompressionChannel = new NotificationChannel("compression", c.getString(R.string.video_compression_channel_name), NotificationManager.IMPORTANCE_LOW);
    videoCompressionChannel.setShowBadge(false);
    videoCompressionChannel.setGroup("status");
    notificationManager.createNotificationChannel(videoCompressionChannel);
    final NotificationChannel exportChannel = new NotificationChannel("backup", c.getString(R.string.backup_channel_name), NotificationManager.IMPORTANCE_LOW);
    exportChannel.setShowBadge(false);
    exportChannel.setGroup("status");
    notificationManager.createNotificationChannel(exportChannel);
    final NotificationChannel incomingCallsChannel = new NotificationChannel(INCOMING_CALLS_NOTIFICATION_CHANNEL, c.getString(R.string.incoming_calls_channel_name), NotificationManager.IMPORTANCE_HIGH);
    incomingCallsChannel.setSound(null, null);
    incomingCallsChannel.setShowBadge(false);
    incomingCallsChannel.setLightColor(LED_COLOR);
    incomingCallsChannel.enableLights(true);
    incomingCallsChannel.setGroup("calls");
    incomingCallsChannel.setBypassDnd(true);
    incomingCallsChannel.enableVibration(false);
    notificationManager.createNotificationChannel(incomingCallsChannel);
    final NotificationChannel ongoingCallsChannel = new NotificationChannel("ongoing_calls", c.getString(R.string.ongoing_calls_channel_name), NotificationManager.IMPORTANCE_LOW);
    ongoingCallsChannel.setShowBadge(false);
    ongoingCallsChannel.setGroup("calls");
    notificationManager.createNotificationChannel(ongoingCallsChannel);
    final NotificationChannel messagesChannel = new NotificationChannel("messages", c.getString(R.string.messages_channel_name), NotificationManager.IMPORTANCE_HIGH);
    messagesChannel.setShowBadge(true);
    messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build());
    messagesChannel.setLightColor(LED_COLOR);
    final int dat = 70;
    final long[] pattern = { 0, 3 * dat, dat, dat };
    messagesChannel.setVibrationPattern(pattern);
    messagesChannel.enableVibration(true);
    messagesChannel.enableLights(true);
    messagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(messagesChannel);
    final NotificationChannel silentMessagesChannel = new NotificationChannel("silent_messages", c.getString(R.string.silent_messages_channel_name), NotificationManager.IMPORTANCE_LOW);
    silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description));
    silentMessagesChannel.setShowBadge(true);
    silentMessagesChannel.setLightColor(LED_COLOR);
    silentMessagesChannel.enableLights(true);
    silentMessagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(silentMessagesChannel);
    final NotificationChannel quietHoursChannel = new NotificationChannel("quiet_hours", c.getString(R.string.title_pref_quiet_hours), NotificationManager.IMPORTANCE_LOW);
    quietHoursChannel.setShowBadge(true);
    quietHoursChannel.setLightColor(LED_COLOR);
    quietHoursChannel.enableLights(true);
    quietHoursChannel.setGroup("chats");
    quietHoursChannel.enableVibration(false);
    quietHoursChannel.setSound(null, null);
    notificationManager.createNotificationChannel(quietHoursChannel);
    final NotificationChannel deliveryFailedChannel = new NotificationChannel("delivery_failed", c.getString(R.string.delivery_failed_channel_name), NotificationManager.IMPORTANCE_DEFAULT);
    deliveryFailedChannel.setShowBadge(false);
    deliveryFailedChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build());
    deliveryFailedChannel.setGroup("chats");
    notificationManager.createNotificationChannel(deliveryFailedChannel);
}
Also used : Context(android.content.Context) NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationManager(android.app.NotificationManager) Builder(androidx.core.app.NotificationCompat.Builder) RequiresApi(androidx.annotation.RequiresApi)

Example 34 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project robolectric by robolectric.

the class ShadowNotificationManagerTest method createNotificationChannelGroup.

@Test
@Config(minSdk = Build.VERSION_CODES.O)
public void createNotificationChannelGroup() {
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("id", "name"));
    assertThat(shadowOf(notificationManager).getNotificationChannelGroups()).hasSize(1);
    NotificationChannelGroup group = (NotificationChannelGroup) shadowOf(notificationManager).getNotificationChannelGroup("id");
    assertThat(group.getName().toString()).isEqualTo("name");
}
Also used : NotificationChannelGroup(android.app.NotificationChannelGroup) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 35 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project AgileDev by LZ9.

the class NotificationUtils method notificationChannelBuild.

/**
 * 如何创建一个NotificationChannel
 */
private void notificationChannelBuild() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannelGroup group = new NotificationChannelGroup("g0001", "测试分组");
        String channelId = "c00001";
        String channelName = "测试通道";
        int level = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, channelName, level);
        // 开启指示灯,如果设备有的话。
        channel.enableLights(true);
        // 设置指示灯颜色
        channel.setLightColor(Color.RED);
        // 通道描述
        channel.setDescription("通道描述");
        // 开启震动
        channel.enableVibration(true);
        // 设置震动频率
        channel.setVibrationPattern(new long[] { 100, 200, 400, 300, 100 });
        channel.setGroup(group.getId());
        // 检测是否绕过免打扰模式
        channel.canBypassDnd();
        // 设置绕过免打扰模式
        channel.setBypassDnd(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        // 检测是否显示角标
        channel.canShowBadge();
        // 设置是否显示角标
        channel.setShowBadge(true);
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup)

Aggregations

NotificationChannelGroup (android.app.NotificationChannelGroup)46 NotificationChannel (android.app.NotificationChannel)31 Test (org.junit.Test)20 TargetApi (android.annotation.TargetApi)8 Preference (android.support.v7.preference.Preference)6 PreferenceCategory (android.support.v7.preference.PreferenceCategory)6 LayoutPreference (com.android.settings.applications.LayoutPreference)6 MasterSwitchPreference (com.android.settings.widget.MasterSwitchPreference)6 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)6 FooterPreference (com.android.settingslib.widget.FooterPreference)6 NotificationManager (android.app.NotificationManager)5 Notification (android.app.Notification)4 AudioAttributes (android.media.AudioAttributes)4 ArrayList (java.util.ArrayList)4 Context (android.content.Context)2 Uri (android.net.Uri)2 RequiresApi (androidx.annotation.RequiresApi)2 Preference (androidx.preference.Preference)2 JSONObject (org.json.JSONObject)2 Application (android.app.Application)1