Android 本地通知 BootReceiver 不工作

Android Local Notification BootReceiver not working

我正在使用一个在特定时间向用户显示通知的应用程序。不幸的是,应用程序在重新启动 phone 后不会显示通知。这是我的代码的样子:

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="mivs.notificationtest" android:installLocation="auto">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk android:minSdkVersion="25" android:targetSdkVersion="30" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<service android:name="com.mivs.notificationtest.app.RebootService"/> <receiver android:name="com.mivs.notificationtest.app.RebootReceiver">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter> </receiver>
</application> </manifest>

RebootReceiver.cs:

[BroadcastReceiver(Enabled =true, Name ="com.mivs.notificationtest.RebootReceiver")]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
public class RebootReceiver : BroadcastReceiver
{
    public Class AlarmService { get; private set; }

    public override void OnReceive(Context context, Intent intent)
    {
        
        if (intent.Action.Equals(Intent.ActionBootCompleted))
        {
            Toast.MakeText(context, "Action Boot Completed!", ToastLength.Long).Show();
            Intent serviceIntent = new Intent(context, typeof(RebootService));
            context.StartService(serviceIntent);

            string title = "If you see this";
            string message = "Its work";

            Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
            alarmIntent.PutExtra("message", message);
            alarmIntent.PutExtra("title", title);

            var pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
            var alarmManager = context.GetSystemService(AlarmService).JavaCast<AlarmManager>();

            DateTime nowDate = DateTime.Now;

            int month = 09;
            int day = 29;
            int hour = 15;
            int minute = 35;

            DateTime dt = new DateTime(nowDate.Year, month, day, hour, minute, 0);
            DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dt.ToString());
            var millisec = dateOffsetValue.ToUnixTimeMilliseconds();

            alarmManager.Set(AlarmType.Rtc, millisec, pendingIntent);
        }
    }
}

RebootService.cs:

[Service(Name = "com.mivs.notificationtest.RebootService")]
public class RebootService : Service
{       
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        Toast.MakeText(this, "Service STARTED!", ToastLength.Long).Show();
        return StartCommandResult.Sticky;
    }

    public override void OnDestroy()
    {
        base.OnDestroy();

        Toast.MakeText(this, "Service STOPED", ToastLength.Long).Show();
    }
}

我已经尝试了在互联网上可以找到的所有相关信息。这些应用程序在 LG G6 Android 9.0 设备上进行了测试。请帮忙。

我还使用了这个 material 来自这个 post:

我刚刚在我的设备上进行了测试。这对我有用:

将此权限添加到清单:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

然后创建这个引导接收器:

[BroadcastReceiver(
    Enabled = true,
    Permission = "android.permission.RECEIVE_BOOT_COMPLETED",
    Exported = true)]
[IntentFilter(new []
{
    "android.intent.action.BOOT_COMPLETED", 
    "android.intent.action.QUICKBOOT_POWERON", 
    "com.htc.intent.action.QUICKBOOT_POWERON"
}, Categories = new []{ "android.intent.category.DEFAULT" })]
public class MyRebootReceiver : BroadcastReceiver
{
    private const string NotificationChannelId = "boot_notifications";
    private const int NotificationId = 1000;
    
    public override void OnReceive(Context? context, Intent? intent)
    {
        Log.Info("MyRebootReceiver", "Got intent");
        
        var notificationManager = (NotificationManager) context?.GetSystemService(Context.NotificationService);
        SetupNotificationChannel(notificationManager);
        
        var resultIntent = new Intent(context, typeof(MainActivity));
        var stackBuilder = TaskStackBuilder.Create(context);
        stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
        stackBuilder.AddNextIntent(resultIntent);

        // Create the PendingIntent with the back stack:
        var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int) PendingIntentFlags.UpdateCurrent);

        var notification = new NotificationCompat.Builder(context, NotificationChannelId)
            .SetContentTitle("Device Rebooted")
            .SetContentText("Your device rebooted")
            .SetSmallIcon(Resource.Drawable.ic_stat_accessibility_new)
            .SetContentIntent(resultPendingIntent);
            
        notificationManager?.Notify(NotificationId, notification.Build());
    }
    
    private void SetupNotificationChannel(NotificationManager notificationManager)
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O) return;

        var channel = new NotificationChannel(NotificationChannelId, "Boot Notifications",
            NotificationImportance.Default)
        {
            Description = "Channel for receiving boot notifications"
        };
            
        notificationManager.CreateNotificationChannel(channel);
    }
}

您只需确保应用程序至少启动过一次。