Exemplo n.º 1
0
 public PushNotificationStyles(NotificationCompat.Style style)
 {
     Id            = 0;
     AutoCancel    = true;
     SoundUri      = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
     Priority      = NotificationPriority.Default;
     Style         = style;
     IconRes       = Application.Context.ApplicationInfo.Icon;
     IconArgbColor = Notification.ColorDefault;
 }
        public static void SendLocalNotification(Context ctx, string notificationChannelId, string title, int smallIconResId, Color smallIconColor, string notificationCatagory,
                                                 int notificationVisibility, int notificationId = 0, string notificationTag = "Edison", PendingIntent pendingIntent = null, bool autoCancel = true,
                                                 string summaryContent = null, int contentIconResId = 0, int color = -1, NotificationCompat.Style style = null, string groupKey = null)
        {
            var notificationManager = NotificationManager.FromContext(ctx);
            var channel             = notificationManager.GetNotificationChannel(notificationChannelId);

            if (channel == null)
            {
                return;
            }

            //Map channel importance (Android 8+) to notification importance (android 7)
            int priority = NotificationCompat.PriorityDefault;

            switch (channel.Importance)
            {
            case NotificationImportance.Max:
                priority = NotificationCompat.PriorityMax;
                break;

            case NotificationImportance.High:
                priority = NotificationCompat.PriorityHigh;
                break;

            case NotificationImportance.Low:
                priority = NotificationCompat.PriorityLow;
                break;

            case NotificationImportance.Min:
                priority = NotificationCompat.PriorityMin;
                break;

            default:
                priority = NotificationCompat.PriorityDefault;
                break;
            }
            // ensure visibility is valid
            if (notificationVisibility > NotificationCompat.VisibilityPublic || notificationVisibility < NotificationCompat.VisibilitySecret)
            {
                notificationVisibility = NotificationCompat.VisibilityPrivate;
            }

            var notificationBuilder = new NotificationCompat.Builder(ctx, notificationChannelId)
                                      .SetContentTitle(title)
                                      .SetSmallIcon(smallIconResId)
                                      .SetColor(smallIconColor)
                                      .SetPriority(priority)
                                      .SetCategory(notificationCatagory)
                                      .SetVisibility(notificationVisibility)
                                      .SetAutoCancel(autoCancel);

            if (!string.IsNullOrWhiteSpace(summaryContent))
            {
                notificationBuilder.SetContentText(summaryContent);
            }
            if (pendingIntent != null)
            {
                notificationBuilder.SetContentIntent(pendingIntent);
            }

            Bitmap icon = null;

            if (contentIconResId != 0)
            {
                if (color != -1)
                {
                    Color col = new Color(color);
                    if (contentIconResId != 0)
                    {
                        // build circular icon
                        Drawable iconDrawable = Drawables.CircularIcon(ctx, contentIconResId, Color.White, col, PixelSizeConverter.DpToPx(15), PixelSizeConverter.DpToPx(140));
                        // try to get bitmap
                        icon = iconDrawable.ToBitmap();
                    }
                }
                else
                {
                    // try to get bitmap
                    icon = BitmapFactory.DecodeResource(ctx.Resources, contentIconResId);
                }

                if (icon != null)
                {
                    notificationBuilder.SetLargeIcon(icon);
                }
            }

            if (style != null)
            {
                notificationBuilder.SetStyle(style);
            }

            if (Build.VERSION.SdkInt >= BuildVersionCodes.N && !string.IsNullOrWhiteSpace(groupKey))
            {
                notificationBuilder.SetGroup(groupKey);
            }

            notificationManager.Notify(notificationTag, notificationId, notificationBuilder.Build());

            icon?.Dispose();
        }