Esempio n. 1
0
        public override void OnReceive(Context context, Intent intent)
        {
            var extra   = intent.GetStringExtra(NotificationKey);
            var id      = intent.GetStringExtra(NotificationBuilder.NotificationId);
            var options = DeserializeNotification(extra);

            if (!string.IsNullOrEmpty(options.AndroidOptions.HexColour) && options.AndroidOptions.HexColour.Substring(0, 1) != "#")
            {
                options.AndroidOptions.HexColour = "#" + options.AndroidOptions.HexColour;
            }

            // Show Notification
            Android.App.Notification.Builder builder = new Android.App.Notification.Builder(Application.Context)
                                                       .SetContentTitle(options.AndroidOptions.DebugShowIdInTitle ? "[" + id + "] " + options.Title : options.Title)
                                                       .SetContentText(options.Description)
                                                       .SetSmallIcon(options.AndroidOptions.SmallDrawableIcon.Value) // Must have small icon to display
                                                       .SetPriority((int)NotificationPriority.High)                  // Must be set to High to get Heads-up notification
                                                       .SetDefaults(NotificationDefaults.All)                        // Must also include vibrate to get Heads-up notification
                                                       .SetAutoCancel(true)
                                                       .SetColor(Color.ParseColor(options.AndroidOptions.HexColour));

            if (options.AndroidOptions.ForceOpenAppOnNotificationTap)
            {
                var clickIntent = new Intent(NotificationBuilder.OnClickIntent);
                clickIntent.PutExtra(NotificationBuilder.NotificationId, int.Parse(id));
                clickIntent.PutExtra(NotificationBuilder.NotificationForceOpenApp, options.AndroidOptions.ForceOpenAppOnNotificationTap);
                var pendingClickIntent = PendingIntent.GetBroadcast(Application.Context, (NotificationBuilder.StartId + int.Parse(id)), clickIntent, 0);
                builder.SetContentIntent(pendingClickIntent);
            }

            // Notification Channel
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                var notificationChannelId = NotificationBuilder.GetOrCreateChannel(options.AndroidOptions.ChannelOptions);
                if (!string.IsNullOrEmpty(notificationChannelId))
                {
                    builder.SetChannelId(notificationChannelId);
                }
            }

            Android.App.Notification notification = builder.Build();

            NotificationManager notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

            notificationManager.Notify(Convert.ToInt32(id), notification);
        }
        public INotificationResult Notify(Activity activity, INotificationOptions options)
        {
            INotificationResult notificationResult = null;

            if (options != null)
            {
                var notificationId = 0;
                var id             = "";
                lock (_lock)
                {
                    notificationId = _count;
                    id             = _count.ToString();
                    _count++;
                }

                int smallIcon;

                if (options.AndroidOptions.SmallDrawableIcon.HasValue)
                {
                    smallIcon = options.AndroidOptions.SmallDrawableIcon.Value;
                }
                else if (_androidOptions.SmallIconDrawable.HasValue)
                {
                    smallIcon = _androidOptions.SmallIconDrawable.Value;
                }
                else
                {
                    smallIcon = Android.Resource.Drawable.IcDialogInfo; // As last resort
                }
                if (options.DelayUntil.HasValue)
                {
                    options.AndroidOptions.SmallDrawableIcon = smallIcon;
                    ScheduleNotification(id, options);
                    return(new NotificationResult()
                    {
                        Action = NotificationAction.NotApplicable, Id = notificationId
                    });
                }

                // Show Notification Right Now
                var dismissIntent = new Intent(DismissedClickIntent);
                dismissIntent.PutExtra(NotificationId, notificationId);

                var pendingDismissIntent = PendingIntent.GetBroadcast(Application.Context, (StartId + notificationId), dismissIntent, 0);

                var clickIntent = new Intent(OnClickIntent);
                clickIntent.PutExtra(NotificationId, notificationId);
                clickIntent.PutExtra(NotificationForceOpenApp, options.AndroidOptions.ForceOpenAppOnNotificationTap);

                // Add custom args
                if (options.CustomArgs != null)
                {
                    foreach (var arg in options.CustomArgs)
                    {
                        clickIntent.PutExtra(arg.Key, arg.Value);
                    }
                }

                var pendingClickIntent = PendingIntent.GetBroadcast(Application.Context, (StartId + notificationId), clickIntent, 0);

                if (!string.IsNullOrEmpty(options.AndroidOptions.HexColor) && options.AndroidOptions.HexColor.Substring(0, 1) != "#")
                {
                    options.AndroidOptions.HexColor = "#" + options.AndroidOptions.HexColor;
                }

                Android.App.Notification.Builder builder = new Android.App.Notification.Builder(Application.Context)
                                                           .SetContentTitle(options.Title)
                                                           .SetContentText(options.Description)
                                                           .SetSmallIcon(smallIcon)                     // Must have small icon to display
                                                           .SetPriority((int)NotificationPriority.High) // Must be set to High to get Heads-up notification
                                                           .SetDefaults(NotificationDefaults.All)       // Must also include vibrate to get Heads-up notification
                                                           .SetAutoCancel(true)                         // To allow click event to trigger delete Intent
                                                           .SetContentIntent(pendingClickIntent)        // Must have Intent to accept the click
                                                           .SetDeleteIntent(pendingDismissIntent)
                                                           .SetColor(Color.ParseColor(options.AndroidOptions.HexColor));

                try
                {
                    // Notification Channel
                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                    {
                        var notificationChannelId = GetOrCreateChannel(options.AndroidOptions.ChannelOptions);
                        if (!string.IsNullOrEmpty(notificationChannelId))
                        {
                            builder.SetChannelId(notificationChannelId);
                        }
                    }
                }
                catch { }
                // System.MissingMethodException: Method 'Android.App.Notification/Builder.SetChannelId' not found.
                // I know this is bad, but I can't replicate it on any version, and many people are experiencing it.

                Android.App.Notification notification = builder.Build();

                NotificationManager notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

                notificationManager.Notify(notificationId, notification);

                if (options.DelayUntil.HasValue)
                {
                    return(new NotificationResult()
                    {
                        Action = NotificationAction.NotApplicable, Id = notificationId
                    });
                }

                var timer = new Timer(x => TimerFinished(id, options.ClearFromHistory, options.AllowTapInNotificationCenter), null, TimeSpan.FromSeconds(7), TimeSpan.FromMilliseconds(-1));

                var resetEvent = new ManualResetEvent(false);
                ResetEvent.Add(id, resetEvent);

                resetEvent.WaitOne(); // Wait for a result

                notificationResult = EventResult[id];

                if (!options.IsClickable && notificationResult.Action == NotificationAction.Clicked)
                {
                    notificationResult.Action = NotificationAction.Dismissed;
                    notificationResult.Id     = notificationId;
                }

                if (EventResult.ContainsKey(id))
                {
                    EventResult.Remove(id);
                }
                if (ResetEvent.ContainsKey(id))
                {
                    ResetEvent.Remove(id);
                }

                // Dispose of Intents and Timer
                pendingClickIntent.Cancel();
                pendingDismissIntent.Cancel();
                timer.Dispose();
            }
            return(notificationResult);
        }