This commit is contained in:
David Britch 2021-02-12 16:20:45 +00:00 коммит произвёл GitHub
Родитель 358691716b
Коммит 5e2d0cf8fb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 61 добавлений и 10 удалений

Просмотреть файл

@ -12,7 +12,8 @@ namespace LocalNotifications.Droid
string title = intent.GetStringExtra(AndroidNotificationManager.TitleKey);
string message = intent.GetStringExtra(AndroidNotificationManager.MessageKey);
AndroidNotificationManager.Instance.Show(title, message);
AndroidNotificationManager manager = AndroidNotificationManager.Instance ?? new AndroidNotificationManager();
manager.Show(title, message);
}
}
}

Просмотреть файл

@ -29,10 +29,15 @@ namespace LocalNotifications.Droid
public static AndroidNotificationManager Instance { get; private set; }
public AndroidNotificationManager() => Initialize();
public void Initialize()
{
CreateNotificationChannel();
Instance = this;
if (Instance == null)
{
CreateNotificationChannel();
Instance = this;
}
}
public void SendNotification(string title, string message, DateTime? notifyTime = null)
@ -89,6 +94,14 @@ namespace LocalNotifications.Droid
manager.Notify(messageId++, notification);
}
public void DeleteNotification(int id)
{
Intent intent = new Intent(AndroidApp.Context, typeof(AlarmHandler));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, id, intent, PendingIntentFlags.CancelCurrent);
AlarmManager alarmManager = AndroidApp.Context.GetSystemService(Context.AlarmService) as AlarmManager;
alarmManager.Cancel(pendingIntent);
}
void CreateNotificationChannel()
{
manager = (NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService);

Просмотреть файл

@ -0,0 +1,19 @@
using Android.App;
using Android.Content;
namespace LocalNotifications.Droid
{
[BroadcastReceiver(Enabled = true, Label = "Reboot complete receiver")]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == "android.intent.action.BOOT_COMPLETED")
{
// Recreate alarms
}
}
}
}

Просмотреть файл

@ -60,6 +60,7 @@
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AlarmHandler.cs" />
<Compile Include="BootReceiver.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />

Просмотреть файл

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.localnotifications">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="29" />
<application android:label="LocalNotifications.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

Просмотреть файл

@ -11,7 +11,7 @@ namespace LocalNotifications.iOS
{
global::Xamarin.Forms.Forms.Init();
// set a delegate to handle incoming notifications
// Set a delegate to handle incoming notifications.
UNUserNotificationCenter.Current.Delegate = new iOSNotificationReceiver();
LoadApplication(new App());

Просмотреть файл

@ -6,13 +6,29 @@ namespace LocalNotifications.iOS
{
public class iOSNotificationReceiver : UNUserNotificationCenterDelegate
{
// Called if app is in the foreground.
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
DependencyService.Get<INotificationManager>().ReceiveNotification(notification.Request.Content.Title, notification.Request.Content.Body);
// alerts are always shown for demonstration but this can be set to "None"
// to avoid showing alerts if the app is in the foreground
ProcessNotification(notification);
completionHandler(UNNotificationPresentationOptions.Alert);
}
// Called if app is in the background, or killed state.
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
if (response.IsDefaultAction)
{
ProcessNotification(response.Notification);
}
completionHandler();
}
void ProcessNotification(UNNotification notification)
{
string title = notification.Request.Content.Title;
string message = notification.Request.Content.Body;
DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);
}
}
}