gpt4 book ai didi

android - 在 Android 的 xamarin 表单中使用警报管理器安排通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:41 25 4
gpt4 key购买 nike

  • 我已经创建了一个依赖项来显示通知

  • 在我的 DeviceDetails_Droid.cs 中我设置了 30 秒的闹钟

  • 本地通知功能在应用程序运行时完美运行活跃但当我杀死应用程序(关闭应用程序)时,警报接收器没有接到电话。


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 中

[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);
}
}

  • 在 list 文件中

<receiver 
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote"
android:label="AlarmReceiver">

  • 上面的代码在应用程序处于运行状态时完美运行 但是当应用程序关闭或终止时,通知不起作用

最佳答案

1) 如果有人终止了您的应用程序,注册到您的应用程序的警报将被取消。

2) 您可以在设备启动 时在后台启动您的服务以注册您的警报,或运行您需要的任何其他代码来设置您的通知...

  • 将“android.intent.action.BOOT_COMPLETED”添加到您的 BroadcastReceiver:

[BroadcastReceiver]
[IntentFilter(new string[]{"android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver

  • 在您的 list 中添加启动完成权限:

<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) 您可以使用 ServiceSeviceIntent 并覆盖 StartCommandResult 以返回 Sticky

对于基于粘性的服务,如果它被终止,它会重新启动。

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.Sticky;
}

关于android - 在 Android 的 xamarin 表单中使用警报管理器安排通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com