0

Using AlarmManager application creates a notification in concrete time. We faced with a problem, from users' words notifications not appear or appear without any sound (settings are fine).

Please tell us what can it be connected with (users say that messengers applications create notifications without any problem)?

Can be the reason of it that we use NOT Notification.Builder and NotificationManager, but AppCompat alternatives: NotificationCompat.Builder and NotificationManagerCompat?

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using System.Threading;
using Android.Support.V4.Content;
using ME.Leolin.Shortcutbadger;
using Android.Media;

namespace WeeklyApp
{
    [BroadcastReceiver]
    public class WeeklyAlarmReceiver : WakefulBroadcastReceiver
    {
    private NotificationCompat.Builder builder;
    private NotificationManagerCompat manager;

    public static readonly int millisForPause = 1000;
    private SettingsWorker settingsWorker;
    private DealsDBWorker dealsDBWorker;
    private int notificationValue;
    private int soundValue;
    private int badgeValue;
    private DateTime currentDate;
    private string currentStringDate;
    private long currentTime;
    private List<Task> currentDayTasks;
    private Task notificationTask;
    private DateTime startingUnixTime = new DateTime(1970, 1, 1);
    private string defaultTitleText;
    private string applicationName;
    private string notificationText;
    private string dealNotificationTime;
    private int pendingIntentId = 0;
    private int maxDealTextLength = 100;
    private string dealNotificationText;
    private DealsDBWorker dbWorker;
    private DatesWorker datesWorker;
    public static int notificationNumber = 0;

    public static List<int> taskIDS;
    private Intent mainIntent;
    private static int currentAPIVersion = (int)Build.VERSION.SdkInt;

    public WeeklyAlarmReceiver()
    {
        defaultTitleText = Application.Context.GetString(Resource.String.NotificationDefaultText);
        applicationName = Application.Context.GetString(Resource.String.ApplicationName);
    }

    public override void OnReceive(Context context, Intent intent)
    {
        settingsWorker = new SettingsWorker();
        dealsDBWorker = new DealsDBWorker();
        mainIntent = new Intent(Application.Context, typeof(MainActivity));
        mainIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
        mainIntent.SetFlags(ActivityFlags.BroughtToFront | ActivityFlags.ClearTop);

        notificationValue = settingsWorker.GetNotificationsValue();
        soundValue = settingsWorker.GetSoundValue();
        badgeValue = settingsWorker.GetShowCounterValue();

        if (builder == null)
        {
            builder = new NotificationCompat.Builder(context);
        }

        builder.SetContentTitle(applicationName);
        manager = NotificationManagerCompat.From(Application.Context);

        if (currentAPIVersion >= 21)
        {
            builder.SetSmallIcon(Resource.Drawable.ic_notification);
        }            
        else
        {
            builder.SetSmallIcon(Resource.Drawable.ic_launcher);
        }

        GetCurrentNotificationTask();

        if (taskIDS == null)
        {
            taskIDS = new List<int>();
        }

        if (notificationTask != null)
        {
            CreateNotification(context);
        }
    }

    private void CreateNotification(Context context)
    {
        mainIntent.PutExtra("DealIdToShow", notificationTask.Id);
        DateTime date = DateTime.Parse(notificationTask.TaskDate);

        PendingIntent pendingIntent = PendingIntent.GetActivity(context, notificationTask.Id, mainIntent, PendingIntentFlags.UpdateCurrent);
        builder.SetContentIntent(pendingIntent);

        GetDealNotificationTime();
        GetDealNotificationText();

        notificationText = string.Format(defaultTitleText, dealNotificationTime, dealNotificationText);

        builder.SetContentText(notificationText);
        builder.SetStyle(new NotificationCompat.BigTextStyle().BigText(notificationText));
        builder.SetAutoCancel(true);

        SetNotification();
    }

    private void SetNotification()
    {
        if (soundValue != 0)
        {
            builder.SetVibrate(new long[] { 10, 1200 });
            builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));

        }
        if (notificationValue != 0)
        {
            notificationNumber++;
            taskIDS.Add(notificationTask.Id);

            if (Build.Manufacturer.Equals("Xiaomi", StringComparison.InvariantCultureIgnoreCase) && badgeValue != 0)
            {
                ShortcutBadger.RemoveCount(Application.Context);
                manager.Cancel(101);
                if(notificationNumber != 0)
                {
                    //ShortcutBadger.ApplyNotification(Application.Context, resultNotification, notificationNumber);
                    ShortcutBadger.ApplyNotification(Application.Context, builder.Build(), notificationNumber);
                }
                else
                {
                    ShortcutBadger.RemoveCount(Application.Context);
                }
            }

            manager.Notify(101, builder.Build());
        }
    }

    private void GetDealNotificationText()
    {
        int textLength = notificationTask.Text.Length;
        if (textLength <= maxDealTextLength)
        {
            dealNotificationText = notificationTask.Text;
        }
        else
        {
            dealNotificationText = $"{notificationTask.Text.Substring(0, maxDealTextLength)}...";
        }

    }

    private void GetDealNotificationTime()
    {
        DateTime resultTime = startingUnixTime.AddSeconds(notificationTask.TaskTime);
        string defaultString = "{0}:{1}";

        if (resultTime.Minute < 10)
        {
            defaultString = "{0}:0{1}";
        }

        dealNotificationTime = string.Format(defaultString, resultTime.Hour, resultTime.Minute);
    }

    private Task GetCurrentNotificationTask()
    {
        currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
        currentStringDate = currentDate.Date.ToString().Remove(10, 8);
        currentDayTasks = dealsDBWorker.GetTasks(currentStringDate, true);
        currentTime = (long)(currentDate - startingUnixTime).TotalSeconds;

        if (currentDayTasks != null)
        {
            notificationTask = Array.Find(currentDayTasks.ToArray(), (a => a.TaskTime == currentTime));
            if (notificationTask != null && (notificationTask.TaskTime % 10) != 0)
            {
                notificationTask = null;
            }
        }
        else
        {
            notificationTask = null;
        }

        return notificationTask;
    }
}

}

TT_W
  • 78
  • 6
  • Can you show us your code? – mjwills Aug 15 '17 at 11:59
  • Have you try to debug your code? Your words "`settings are fine`" means `soundValue != 0` return true and builder have use `SetSound` method, but your `Notification` still appear without any sound? – York Shen Aug 17 '17 at 09:54
  • In my devices during debug and during normal using everything works in right way. Other users say that they have problems. Settings are fine mean that users' devices don't prohibit notifications. We found out that some other applications can block our notifications. Thank for your reply. – TT_W Aug 17 '17 at 15:04
  • You could read this : https://stackoverflow.com/questions/11649151/android-4-1-how-to-check-notifications-are-disabled-for-the-application – York Shen Aug 21 '17 at 01:22

0 Answers0