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

标签 android xamarin xamarin.android local

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

  • 在我的 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/

相关文章:

android - 什么时候使用和不使用 android Application 类?

在Android中运行TensorFlow模型时出现java.nio.BufferOverflowException运行时错误?

php - 如何从 JSON 编码的字符串中修剪换行符?

android - 相对布局 : Different behavior on Api < 11

c# - UWP 中的 Xamarin Forms MasterDetailPage 按钮填充问题

c# - Xamarin 的当前上下文中不存在名称 'Log'?

dll - 为什么 Xamarin.Android 重新映射程序集请求?

c# - 如何检查 PCL 项目 xamarin 中的服务器是否可达?

c# - 根据屏幕大小更改标签大小 xamarin forms

c# - 在 visual studio 2015 中发布 xamarin 应用程序