【发布时间】:2018-04-24 05:03:18
【问题描述】:
https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. 我正在使用这个后台工作者。我想在我的应用程序状态运行与否的每一天重复显示本地通知。它对我不起作用..请帮忙。 SendNotification() 正在从主要活动中调用。重复的本地通知在我的应用程序状态正在运行的地方运行良好。但它在我的应用程序状态未运行的情况下不起作用。
public async Task SendNotification()
{
Intent alarmIntent = new Intent(this, typeof(AlarmReceiverNew));
alarmIntent.PutExtra(“message”, “message”);
alarmIntent.PutExtra(“title”, “Welcome”);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)this.GetSystemService(Context.AlarmService);
long futureInMillis = SystemClock.ElapsedRealtime() + 12 * 3600 * 1000;
alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, futureInMillis, pendingIntent);
}
[BroadcastReceiver]
public class AlarmReceiverNew : BroadcastReceiver
{
public async override void OnReceive(Context context, Intent intent)
{
Intent notIntent = new Intent(context, typeof(MainActivity));
PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
NotificationManagerCompat manager = NotificationManagerCompat.From(context);
var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, 1));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.icon).SetContentTitle(“title”)
.SetContentText(“message”)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.Extend(wearableExtender);
Notification notification = builder.Build();
manager.Notify(0, notification);
}
}
我也在尝试Android - Running a background task every 15 minutes, even when application is not running 解决方案。但输出相同。
【问题讨论】:
-
尝试使用 Firebase JobDispatcher,更简单
-
@NileshRathod 在我清除了我的应用程序历史记录后它不起作用。(即,应用程序未处于运行状态)。
-
@DijkeMark 你得到解决方案了吗。
标签: android xamarin.forms notifications broadcastreceiver localnotification