问题描述
-
我已经创建了一个依赖项来显示通知
I have created a dependencie to show the notifications
在我的DeviceDetails_Droid.cs中,我将设置闹钟设置为30秒
In My DeviceDetails_Droid.cs I've set set alarm for 30 seconds
当应用程序处于运行状态时,本地通知功能可以完美运行处于活动状态,但是当我杀死该应用程序(关闭应用程序)时,警报接收器没有接到电话.
The functionality for local notification works perfectly when app isactive but when I killed the app (close app) the alarm receivernot getting called.
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);
}
- 在Android的MainActivity中
- In Androids MainActivity
[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);
}
}
- 在清单文件中
- And in manifest file
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote"
android:label="AlarmReceiver">
- 当应用处于运行状态时,上述代码可以完美运行 但是当应用关闭或被终止时,通知不起作用
- The above code is running perfectly when app is in running state But the notification is not working when app is closed or killed
- 在您的BroadcastReceiver中添加"android.intent.action.BOOT_COMPLETED":
推荐答案
1)如果有人杀死了您的应用程序,则注册到您的应用程序的警报将被取消.
1) If someone kills your app, alarms registered to your app are cancelled.
2)您可以在设备的Boot
后台启动服务以注册警报,或运行设置通知的任何其他代码...
2) You can start your service in the background on Boot
of the device in order to register your alarms, or run any other code you need to setup your notifications...
[BroadcastReceiver]
[IntentFilter(new string[]{"android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver
- 在清单中添加启动完成权限:
- In your manifest add in the boot complete permission:
<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的股票价格示例,如果您将"RECEIVE_BOOT_COMPLETED"设置为自动",则会重启服务,并且您的手机重启后,您将开始接收通知,而无需先启动您的应用程序:
In the case of Xamarin's Stock Price example, if you set "RECEIVE_BOOT_COMPLETED" you are "auto" restart your service and the your will start receiving notifications upon reboot of their phone without first launching your app:
[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
3) You can use a Service
vs. a SeviceIntent
and override the StartCommandResult
to return Sticky
对于基于粘性的服务,如果被终止,它将重新启动.
With a Sticky-based Service, it is restarted if it gets terminated.
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.Sticky;
}
这篇关于在Android Xamarin表单中使用警报管理器计划通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!