Xamarin.Android通知无法正常工作

时间:2017-08-15 11:56:21

标签: c# android xamarin xamarin.android

使用AlarmManager应用程序在具体时间创建通知。我们遇到了一个问题,来自用户'单词通知没有出现或没有任何声音显示(设置很好)。

请告诉我们它可以连接什么(用户说messengers应用程序创建通知没有任何问题)?

可能是因为我们使用Not Notification.Builder和NotificationManager,但是AppCompat替代品:NotificationCompat.Builder和NotificationManagerCompat?

这是我的代码:

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;
    }
}

}

0 个答案:

没有答案