我正在尝试从JSON对象加载时间列表,并希望每天在特定时间显示计划通知(在JSON对象中提到)。
要在我的应用中启用此功能,任何人都可以指导我做什么以及如何实现它?
我尝试过简单的AlarmManager和广播接收器,但这并没有触发简单的通知。
所以我的代码如下所示,任何人都可以在下面的代码中指导我,感谢您的帮助。
下面的是AlarmReceiver.cs类
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent notificationIntent = new Intent(context, Java.Lang.Class.FromType(typeof(NotificationActivity)));
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(context);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationActivity)));
stackBuilder.AddNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.GetPendingIntent(0,PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(context);
Notification notification = builder.SetContentTitle("Demo App Notification")
.SetContentText("New Notification From Demo App..")
.SetTicker("New Message Alert!")
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentIntent(pendingIntent).Build();
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notification);
}
}
主要活动
[Activity(Label = "AlarmService", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.AddCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.GetService(this, 100, notificationIntent, PendingIntentFlags.UpdateCurrent);
Calendar cal = Calendar.GetInstance(Locale.Default);
cal.Add(CalendarField.Second, 1);
alarmManager.SetExact(AlarmType.RtcWakeup, cal.TimeInMillis, broadcast);
}
}
通知活动
namespace AlarmService
{
[Activity(Label = "NotificationActivity")]
public class NotificationActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.notification_activity);
// Create your application here
}
}
}
在Xamarin中修改了menifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AlarmService.AlarmService" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application android:label="AlarmService"></application>
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</receiver>