【发布时间】:2021-11-21 14:33:07
【问题描述】:
我正在使用一个在特定时间向用户显示通知的应用程序。不幸的是,重新启动手机后应用程序不会显示通知。这是我的代码的样子:
我的清单:
<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 设备上进行了测试。请帮忙。
我也使用了这篇文章中的这个材料: “Rebooting receiver” not working android [Xamarin.Android]
【问题讨论】:
-
如果您已经使用
[Service]和[BroadcastReceiver]注释它们,则无需在AndroidManifest 中定义您的服务和广播接收器。 Xamarin.Android 将为您添加条目。这可能会造成混乱,因此请从删除手动输入开始。 -
我会尝试删除清单中的消息。我给出的:com.mivs.notificationtest.RebootService 是正确的形式吗?这真的很重要吗?
-
我从清单中删除了代码,但通知仍然不起作用。
标签: android xamarin localnotification