我创建了一个依赖关系来显示通知
在我的DeviceDetails_Droid.cs中我设置闹钟30秒
当app应用时,本地通知的功能非常有效 活跃,但当我杀死应用程序(关闭应用程序)的警报接收器 没有被召唤。
public void ShowNotification(string message, string title)
{
Intent alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra ("message", message);
alarmIntent.PutExtra ("title", title);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager) Forms.Context.GetSystemService(Context.AlarmService);
//TODO: For demo set after 5 seconds.
alarmManager.Set(AlarmType.RtcWakeup, DateTime.Now.Millisecond + 30000, pendingIntent);
}
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive (Context context, Intent intent)
{
var message = intent.GetStringExtra ("message");
var title = intent.GetStringExtra ("title");
var notIntent = new Intent (context, typeof(MainActivity));
var contentIntent = PendingIntent.GetActivity (context, 0, notIntent, PendingIntentFlags.CancelCurrent);
var manager = NotificationManagerCompat.From (context);
var style = new NotificationCompat.BigTextStyle();
style.BigText(message);
//Generate a notification with just short text and small icon
var builder = new NotificationCompat.Builder (context)
.SetContentIntent (contentIntent)
.SetSmallIcon (Resource.Drawable.Icon)
.SetContentTitle (title)
.SetContentText (message)
.SetStyle (style)
.SetWhen (Java.Lang.JavaSystem.CurrentTimeMillis ())
.SetAutoCancel (true);
var notification = builder.Build();
manager.Notify(0, notification);
}
}
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote"
android:label="AlarmReceiver">
答案 0 :(得分:7)
1)如果有人杀死了您的应用,则会取消注册到您应用的闹钟。
2)您可以在设备Boot
的后台启动服务,以便注册您的闹钟,或运行设置通知所需的任何其他代码......
[BroadcastReceiver]
[IntentFilter(new string[]{"android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver
<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>
对于Xamarin的Stock Price example,如果您设置“RECEIVE_BOOT_COMPLETED”,则“自动”重启您的服务,您将在重新启动手机时开始接收通知,而无需先启动您的应用:
[BroadcastReceiver]
[IntentFilter(new string[]{StockService.StocksUpdatedAction,Boo "android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class StockNotificationReceiver : BroadcastReceiver
3)您可以使用Service
与SeviceIntent
并覆盖StartCommandResult
以返回Sticky
使用基于Sticky的服务,如果它被终止则会重新启动。
从onStartCommand(Intent,int,int)返回的常量:如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),则将其保持在启动状态但不要保留这个交付的意图。稍后系统将尝试重新创建服务。因为它处于启动状态,所以它将保证在创建新服务实例后调用onStartCommand(Intent,int,int);如果没有任何挂起的启动命令要传递给服务,它将使用null intent对象调用,因此您必须注意检查这一点。
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.Sticky;
}
答案 1 :(得分:0)