我一直试图在我的android应用程序中实现重复的本地通知,并且我已经做到了这一点(我相信),但是当我想单击通知并在其中执行逻辑时,请执行以下操作:1.不启动MainActivity(不启动我的应用程序)2.不执行任何逻辑
在我的MainActivity.cs文件中,我还将LaunchMode设置为SingleTop,我对方法OnNewIntent()进行了相应的覆盖,当我单击该方法时,它将接收我的通知,但它甚至根本不执行该方法] >
由于我实现了与共享代码项目进行通信的接口的实现,因此我通过在不是MainActivity.cs的其他类中调用的BroadcastReceiver发送通知,这是由于我实现了与共享代码项目进行通信的接口的实现
调用通知的方法:
public void ScheduleNotification(string title, string message, long seconds) { if (!channelInitialized) CreateNotificationChannel(); NotificationReciever.title = title; NotificationReciever.message = message; AlarmManager alarmManager = (AlarmManager)AndroidApp.Context.GetSystemService(Context.AlarmService); Intent intent = new Intent(AndroidApp.Context, typeof(NotificationReciever)); intent.PutExtra(TitleKey, title); intent.PutExtra(MessageKey, message); intent.AddFlags(ActivityFlags.ClearTop); PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 0, intent, PendingIntentFlags.UpdateCurrent); alarmManager.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + seconds * 1000, 2000, pendingIntent); }
我的广播接收者:
[BroadcastReceiver(Enabled =true)] public class NotificationReciever : BroadcastReceiver { const string channelId = "default"; public static string title = ""; public static string message = ""; const string channelName = "Default"; const string channelDescription = "The default channel for notifications."; const int pendingIntentId = 0; public const string TitleKey = "title"; public const string MessageKey = "message"; int messageId = -1; NotificationManager manager; public override void OnReceive(Context context, Intent intent) { PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.OneShot); NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId) .SetContentIntent(pendingIntent) .SetContentTitle(title) .SetContentText(message) .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.OlympiaLogo)) .SetSmallIcon(Resource.Drawable.OlympiaLogo) .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate); manager = (NotificationManager)context.GetSystemService(AndroidApp.NotificationService); var notification = builder.Build(); manager.Notify(messageId, notification); } }
最后是我的MainActivity.cs(当我单击通知时没有执行的那个:]
[Activity(Label = "LocalNotificationsApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); CrossCurrentActivity.Current.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); CreateNotificationFromIntent(Intent); LoadApplication(new App()); // //AndroidNotificationManager.alarmManager = (AlarmManager)GetSystemService(Context.AlarmService); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } protected override void OnNewIntent(Intent intent) { CreateNotificationFromIntent(intent); } void CreateNotificationFromIntent(Intent intent) { if (intent?.Extras != null) { string title = intent.Extras.GetString(AndroidNotificationManager.TitleKey); string message = intent.Extras.GetString(AndroidNotificationManager.MessageKey); if(title != null && message != null) DependencyService.Get<INotificationManager>().ReceiveNotification(title, message); } } }
请我什至不了解问题,我需要紧急帮助!
我一直试图在我的android应用程序中实现重复的本地通知,并且我已经做到了这一点(我相信),但是当我想单击通知并在其中执行逻辑时...
尝试创建全新的意图并使用主要活动,而不是重新使用传递给OnReceived
的活动