Search in sources :

Example 76 with NotificationChannel

use of android.app.NotificationChannel in project Tusky by Vavassor.

the class NotificationHelper method createNotificationChannelsForAccount.

public static void createNotificationChannelsForAccount(@NonNull AccountEntity account, @NonNull 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_FOLLOW_REQUEST + account.getIdentifier(), CHANNEL_BOOST + account.getIdentifier(), CHANNEL_FAVOURITE + account.getIdentifier(), CHANNEL_POLL + account.getIdentifier(), CHANNEL_SUBSCRIPTIONS + account.getIdentifier() };
        int[] channelNames = { R.string.notification_mention_name, R.string.notification_follow_name, R.string.notification_follow_request_name, R.string.notification_boost_name, R.string.notification_favourite_name, R.string.notification_poll_name, R.string.notification_subscription_name };
        int[] channelDescriptions = { R.string.notification_mention_descriptions, R.string.notification_follow_description, R.string.notification_follow_request_description, R.string.notification_boost_description, R.string.notification_favourite_description, R.string.notification_poll_description, R.string.notification_subscription_description };
        List<NotificationChannel> channels = new ArrayList<>(6);
        NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName());
        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);
        }
        notificationManager.createNotificationChannels(channels);
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationManager(android.app.NotificationManager) ArrayList(java.util.ArrayList)

Example 77 with NotificationChannel

use of android.app.NotificationChannel in project kdeconnect-android by KDE.

the class NotificationHelper method initializeChannels.

public static void initializeChannels(Context context) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        return;
    }
    NotificationManager manager = ContextCompat.getSystemService(context, NotificationManager.class);
    NotificationChannel persistentChannel = new NotificationChannel(Channels.PERSISTENT, context.getString(R.string.notification_channel_persistent), NotificationManager.IMPORTANCE_MIN);
    manager.createNotificationChannel(persistentChannel);
    manager.createNotificationChannel(new NotificationChannel(Channels.DEFAULT, context.getString(R.string.notification_channel_default), NotificationManager.IMPORTANCE_DEFAULT));
    manager.createNotificationChannel(new NotificationChannel(Channels.MEDIA_CONTROL, context.getString(R.string.notification_channel_media_control), NotificationManager.IMPORTANCE_LOW));
    NotificationChannel fileTransfer = new NotificationChannel(Channels.FILETRANSFER, context.getString(R.string.notification_channel_filetransfer), NotificationManager.IMPORTANCE_LOW);
    fileTransfer.enableVibration(false);
    manager.createNotificationChannel(fileTransfer);
    manager.createNotificationChannel(new NotificationChannel(Channels.RECEIVENOTIFICATION, context.getString(R.string.notification_channel_receivenotification), NotificationManager.IMPORTANCE_DEFAULT));
    manager.createNotificationChannel(new NotificationChannel(Channels.SMS_MMS, context.getString(R.string.notification_channel_sms_mms), NotificationManager.IMPORTANCE_DEFAULT));
    NotificationChannel highPriority = new NotificationChannel(Channels.HIGHPRIORITY, context.getString(R.string.notification_channel_high_priority), NotificationManager.IMPORTANCE_HIGH);
    manager.createNotificationChannel(highPriority);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager)

Example 78 with NotificationChannel

use of android.app.NotificationChannel 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 79 with NotificationChannel

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

the class NotificationChannelManagerRunner method createNotificationChannelCreateBasicChannel.

@Test
public void createNotificationChannelCreateBasicChannel() throws Exception {
    JSONObject payload = new JSONObject();
    JSONObject chnl = new JSONObject();
    chnl.put("id", "test_id");
    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());
    assertNotNull(channel.getSound());
    assertTrue(channel.shouldShowLights());
    assertTrue(channel.shouldVibrate());
}
Also used : NotificationChannelManager_createNotificationChannel(com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_createNotificationChannel) NotificationChannel(android.app.NotificationChannel) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 80 with NotificationChannel

use of android.app.NotificationChannel 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)

Aggregations

NotificationChannel (android.app.NotificationChannel)762 Test (org.junit.Test)430 NotificationBackend (com.android.settings.notification.NotificationBackend)215 NotificationManager (android.app.NotificationManager)210 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)114 Intent (android.content.Intent)87 Preference (androidx.preference.Preference)76 PendingIntent (android.app.PendingIntent)73 NotificationChannelGroup (android.app.NotificationChannelGroup)54 NotificationCompat (android.support.v4.app.NotificationCompat)45 Notification (android.app.Notification)43 ArrayList (java.util.ArrayList)34 TargetApi (android.annotation.TargetApi)32 AudioAttributes (android.media.AudioAttributes)31 RequiresApi (android.support.annotation.RequiresApi)28 RequiresApi (androidx.annotation.RequiresApi)25 Uri (android.net.Uri)21 RestrictedListPreference (com.android.settings.RestrictedListPreference)20 SuppressLint (android.annotation.SuppressLint)19 ShortcutInfo (android.content.pm.ShortcutInfo)19