Search in sources :

Example 6 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project OneSignal-Android-SDK by OneSignal.

the class NotificationChannelManager method createChannel.

// Creates NotificationChannel and NotificationChannelGroup based on a json payload.
// Returns channel id after it is created.
// Language dependent fields will be passed localized
@RequiresApi(api = Build.VERSION_CODES.O)
private static String createChannel(Context context, NotificationManager notificationManager, JSONObject payload) throws JSONException {
    // 'chnl' will be a string if coming from FCM and it will be a JSONObject when coming from
    // a cold start sync.
    Object objChannelPayload = payload.opt("chnl");
    JSONObject channelPayload = null;
    if (objChannelPayload instanceof String)
        channelPayload = new JSONObject((String) objChannelPayload);
    else
        channelPayload = (JSONObject) objChannelPayload;
    String channel_id = channelPayload.optString("id", DEFAULT_CHANNEL_ID);
    // Ensure we don't try to use the system reserved id
    if (channel_id.equals(NotificationChannel.DEFAULT_CHANNEL_ID))
        channel_id = DEFAULT_CHANNEL_ID;
    JSONObject payloadWithText = channelPayload;
    if (channelPayload.has("langs")) {
        JSONObject langList = channelPayload.getJSONObject("langs");
        String language = LanguageContext.getInstance().getLanguage();
        if (langList.has(language))
            payloadWithText = langList.optJSONObject(language);
    }
    String channel_name = payloadWithText.optString("nm", "Miscellaneous");
    int importance = priorityToImportance(payload.optInt("pri", 6));
    NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
    channel.setDescription(payloadWithText.optString("dscr", null));
    if (channelPayload.has("grp_id")) {
        String group_id = channelPayload.optString("grp_id");
        CharSequence group_name = payloadWithText.optString("grp_nm");
        notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group_id, group_name));
        channel.setGroup(group_id);
    }
    if (payload.has("ledc")) {
        String ledc = payload.optString("ledc");
        Matcher matcher = hexPattern.matcher(ledc);
        BigInteger ledColor;
        if (!matcher.matches()) {
            OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "OneSignal LED Color Settings: ARGB Hex value incorrect format (E.g: FF9900FF)");
            ledc = "FFFFFFFF";
        }
        try {
            ledColor = new BigInteger(ledc, 16);
            channel.setLightColor(ledColor.intValue());
        } catch (Throwable t) {
            OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Couldn't convert ARGB Hex value to BigInteger:", t);
        }
    }
    channel.enableLights(payload.optInt("led", 1) == 1);
    if (payload.has("vib_pt")) {
        long[] vibrationPattern = OSUtils.parseVibrationPattern(payload);
        if (vibrationPattern != null)
            channel.setVibrationPattern(vibrationPattern);
    }
    channel.enableVibration(payload.optInt("vib", 1) == 1);
    if (payload.has("sound")) {
        // Sound will only play if Importance is set to High or Urgent
        String sound = payload.optString("sound", null);
        Uri uri = OSUtils.getSoundUri(context, sound);
        if (uri != null)
            channel.setSound(uri, null);
        else if ("null".equals(sound) || "nil".equals(sound))
            channel.setSound(null, null);
    // null = None for a sound.
    }
    // Setting sound to null makes it 'None' in the Settings.
    // Otherwise not calling setSound makes it the default notification sound.
    channel.setLockscreenVisibility(payload.optInt("vis", Notification.VISIBILITY_PRIVATE));
    channel.setShowBadge(payload.optInt("bdg", 1) == 1);
    channel.setBypassDnd(payload.optInt("bdnd", 0) == 1);
    OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "Creating notification channel with channel:\n" + channel.toString());
    try {
        notificationManager.createNotificationChannel(channel);
    } catch (IllegalArgumentException e) {
        // TODO: Remove this try-catch once it is figured out which argument is causing Issue #895
        // try-catch added to prevent crashing from the illegal argument
        // Added logging above this try-catch so we can evaluate the payload of the next victim
        // to report a stacktrace
        // https://github.com/OneSignal/OneSignal-Android-SDK/issues/895
        e.printStackTrace();
    }
    return channel_id;
}
Also used : NotificationChannelGroup(android.app.NotificationChannelGroup) Matcher(java.util.regex.Matcher) Uri(android.net.Uri) NotificationChannel(android.app.NotificationChannel) JSONObject(org.json.JSONObject) BigInteger(java.math.BigInteger) JSONObject(org.json.JSONObject) RequiresApi(androidx.annotation.RequiresApi)

Example 7 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project OneSignal-Android-SDK by OneSignal.

the class NotificationChannelManagerRunner method createNotificationChannelWithALlOptions.

@Test
public void createNotificationChannelWithALlOptions() throws Exception {
    JSONObject payload = new JSONObject();
    JSONObject chnl = new JSONObject();
    chnl.put("id", "test_id");
    chnl.put("nm", "Test Name");
    chnl.put("dscr", "Some description");
    chnl.put("grp_id", "grp_id");
    chnl.put("grp_nm", "Group Name");
    payload.put("pri", 10);
    payload.put("led", 0);
    payload.put("ledc", "FFFF0000");
    payload.put("vib", 0);
    payload.put("vib_pt", new JSONArray("[1,2,3,4]"));
    payload.put("sound", "notification");
    payload.put("vis", Notification.VISIBILITY_SECRET);
    payload.put("bdg", 1);
    payload.put("bdnd", 1);
    payload.put("chnl", chnl.toString());
    String ret = NotificationChannelManager_createNotificationChannel(blankActivity, payload);
    NotificationChannel channel = ShadowRoboNotificationManager.lastChannel;
    assertEquals("test_id", ret);
    assertEquals("test_id", ShadowRoboNotificationManager.lastChannel.getId());
    assertEquals("Test Name", channel.getName());
    assertEquals("Some description", channel.getDescription());
    assertEquals("grp_id", channel.getGroup());
    NotificationChannelGroup group = ShadowRoboNotificationManager.lastChannelGroup;
    assertEquals("grp_id", group.getId());
    assertEquals("Group Name", group.getName());
    assertNotNull(channel.getSound());
    // Setting a led color should NOT override enableLights
    assertFalse(channel.shouldShowLights());
    assertEquals(-65536, channel.getLightColor());
    // Setting a pattern should NOT override enableVibration
    assertFalse(channel.shouldVibrate());
    assertArrayEquals(new long[] { 1, 2, 3, 4 }, channel.getVibrationPattern());
    assertEquals(NotificationManager.IMPORTANCE_MAX, channel.getImportance());
    assertEquals("content://settings/system/notification_sound", channel.getSound().toString());
    assertEquals(Notification.VISIBILITY_SECRET, channel.getLockscreenVisibility());
    assertTrue(channel.canShowBadge());
    assertTrue(channel.canBypassDnd());
}
Also used : NotificationChannelManager_createNotificationChannel(com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_createNotificationChannel) NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 8 with NotificationChannelGroup

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

the class ShadowNotificationManagerTest method deleteNotificationChannelGroup.

@Test
@Config(minSdk = Build.VERSION_CODES.O)
public void deleteNotificationChannelGroup() {
    final String channelId = "channelId";
    final String channelGroupId = "channelGroupId";
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(channelGroupId, "groupName"));
    NotificationChannel channel = new NotificationChannel(channelId, "channelName", 1);
    channel.setGroup(channelGroupId);
    notificationManager.createNotificationChannel(channel);
    assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse();
    notificationManager.deleteNotificationChannelGroup(channelGroupId);
    assertThat(shadowOf(notificationManager).getNotificationChannelGroup(channelGroupId)).isNull();
    // Per documentation, deleting a channel group also deletes all associated channels.
    assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isTrue();
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 9 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Signal-Android by WhisperSystems.

the class NotificationChannels method isMessagesChannelGroupEnabled.

/**
 * Whether or not the notification category for messages is enabled. Note that even if it is,
 * a user could have blocked the specific channel, or notifications overall, and it'd still be
 * true. See {@link #isMessageChannelEnabled(Context)} and {@link #areNotificationsEnabled(Context)}.
 */
public static synchronized boolean isMessagesChannelGroupEnabled(@NonNull Context context) {
    if (Build.VERSION.SDK_INT < 28) {
        return true;
    }
    NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
    NotificationChannelGroup group = notificationManager.getNotificationChannelGroup(CATEGORY_MESSAGES);
    return group != null && !group.isBlocked();
}
Also used : NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationManager(android.app.NotificationManager)

Example 10 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Signal-Android by WhisperSystems.

the class NotificationChannels method onCreate.

@TargetApi(26)
private static void onCreate(@NonNull Context context, @NonNull NotificationManager notificationManager) {
    NotificationChannelGroup messagesGroup = new NotificationChannelGroup(CATEGORY_MESSAGES, context.getResources().getString(R.string.NotificationChannel_group_chats));
    notificationManager.createNotificationChannelGroup(messagesGroup);
    NotificationChannel messages = new NotificationChannel(getMessagesChannel(context), context.getString(R.string.NotificationChannel_channel_messages), NotificationManager.IMPORTANCE_HIGH);
    NotificationChannel calls = new NotificationChannel(CALLS, context.getString(R.string.NotificationChannel_calls), NotificationManager.IMPORTANCE_HIGH);
    NotificationChannel failures = new NotificationChannel(FAILURES, context.getString(R.string.NotificationChannel_failures), NotificationManager.IMPORTANCE_HIGH);
    NotificationChannel backups = new NotificationChannel(BACKUPS, context.getString(R.string.NotificationChannel_backups), NotificationManager.IMPORTANCE_LOW);
    NotificationChannel lockedStatus = new NotificationChannel(LOCKED_STATUS, context.getString(R.string.NotificationChannel_locked_status), NotificationManager.IMPORTANCE_LOW);
    NotificationChannel other = new NotificationChannel(OTHER, context.getString(R.string.NotificationChannel_other), NotificationManager.IMPORTANCE_LOW);
    NotificationChannel voiceNotes = new NotificationChannel(VOICE_NOTES, context.getString(R.string.NotificationChannel_voice_notes), NotificationManager.IMPORTANCE_LOW);
    NotificationChannel joinEvents = new NotificationChannel(JOIN_EVENTS, context.getString(R.string.NotificationChannel_contact_joined_signal), NotificationManager.IMPORTANCE_DEFAULT);
    NotificationChannel background = new NotificationChannel(BACKGROUND, context.getString(R.string.NotificationChannel_background_connection), getDefaultBackgroundChannelImportance(notificationManager));
    NotificationChannel callStatus = new NotificationChannel(CALL_STATUS, context.getString(R.string.NotificationChannel_call_status), NotificationManager.IMPORTANCE_LOW);
    messages.setGroup(CATEGORY_MESSAGES);
    setVibrationEnabled(messages, SignalStore.settings().isMessageVibrateEnabled());
    messages.setSound(SignalStore.settings().getMessageNotificationSound(), getRingtoneAudioAttributes());
    setLedPreference(messages, SignalStore.settings().getMessageLedColor());
    calls.setShowBadge(false);
    backups.setShowBadge(false);
    lockedStatus.setShowBadge(false);
    other.setShowBadge(false);
    setVibrationEnabled(other, false);
    voiceNotes.setShowBadge(false);
    joinEvents.setShowBadge(false);
    background.setShowBadge(false);
    callStatus.setShowBadge(false);
    notificationManager.createNotificationChannels(Arrays.asList(messages, calls, failures, backups, lockedStatus, other, voiceNotes, joinEvents, background, callStatus));
    if (BuildConfig.PLAY_STORE_DISABLED) {
        NotificationChannel appUpdates = new NotificationChannel(APP_UPDATES, context.getString(R.string.NotificationChannel_app_updates), NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(appUpdates);
    } else {
        notificationManager.deleteNotificationChannel(APP_UPDATES);
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) TargetApi(android.annotation.TargetApi)

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