NotificationUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.utils;
  21. import android.annotation.TargetApi;
  22. import android.app.NotificationChannel;
  23. import android.app.NotificationChannelGroup;
  24. import android.app.NotificationManager;
  25. import android.content.Context;
  26. import android.graphics.Color;
  27. import android.os.Build;
  28. import android.service.notification.StatusBarNotification;
  29. import com.nextcloud.talk.R;
  30. import com.nextcloud.talk.models.database.UserEntity;
  31. import java.util.zip.CRC32;
  32. public class NotificationUtils {
  33. public static final String NOTIFICATION_CHANNEL_CALLS = "NOTIFICATION_CHANNEL_CALLS";
  34. public static final String NOTIFICATION_CHANNEL_MESSAGES = "NOTIFICATION_CHANNEL_MESSAGES";
  35. public static final String NOTIFICATION_CHANNEL_CALLS_V2 = "NOTIFICATION_CHANNEL_CALLS_V2";
  36. public static final String NOTIFICATION_CHANNEL_MESSAGES_V2 = "NOTIFICATION_CHANNEL_MESSAGES_V2";
  37. public static final String NOTIFICATION_CHANNEL_MESSAGES_V3 = "NOTIFICATION_CHANNEL_MESSAGES_V2";
  38. @TargetApi(Build.VERSION_CODES.O)
  39. public static void createNotificationChannel(NotificationManager notificationManager,
  40. String channelId, String channelName,
  41. String channelDescription, boolean enableLights,
  42. int importance) {
  43. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O
  44. && notificationManager.getNotificationChannel(channelId) == null) {
  45. NotificationChannel channel = new NotificationChannel(channelId, channelName,
  46. importance);
  47. channel.setDescription(channelDescription);
  48. channel.enableLights(enableLights);
  49. channel.setLightColor(Color.RED);
  50. notificationManager.createNotificationChannel(channel);
  51. }
  52. }
  53. @TargetApi(Build.VERSION_CODES.O)
  54. public static void createNotificationChannelGroup(NotificationManager notificationManager,
  55. String groupId, CharSequence groupName) {
  56. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  57. NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup(groupId, groupName);
  58. if (!notificationManager.getNotificationChannelGroups().contains(notificationChannelGroup)) {
  59. notificationManager.createNotificationChannelGroup(notificationChannelGroup);
  60. }
  61. }
  62. }
  63. public static void cancelExistingNotifications(Context context, UserEntity conversationUser) {
  64. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
  65. NotificationManager notificationManager =
  66. (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  67. CRC32 crc32 = new CRC32();
  68. String groupName = String.format(context.getResources().getString(R.string
  69. .nc_notification_channel), conversationUser.getUserId(), conversationUser.getBaseUrl());
  70. crc32.update(groupName.getBytes());
  71. String crc32GroupString = Long.toString(crc32.getValue());
  72. if (notificationManager != null) {
  73. StatusBarNotification statusBarNotifications[] = notificationManager.getActiveNotifications();
  74. for (StatusBarNotification statusBarNotification : statusBarNotifications) {
  75. if (statusBarNotification.getGroupKey().equals(crc32GroupString)) {
  76. notificationManager.cancel(statusBarNotification.getId());
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }