通知后唤醒并在退出xamarin后删除

时间:2016-07-27 04:11:15

标签: android xamarin

我需要在收到通知后唤醒android并在从xamarin中的app退出后删除它们。如果手机未处于睡眠模式通知状态正常,但在转入睡眠状态后,我只能听到通知声音而无其他声音。

AlarmReciever.cs

public class AlarmReceiver : BroadcastReceiver
    {
        NotificationManager manager;
        public override void OnReceive(Context context, Intent intent)
        {

            string message = intent.GetStringExtra("message");
            string title = intent.GetStringExtra("title");

            Intent notIntent = new Intent(context, typeof(MainActivity));
            PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
            manager = NotificationManager.FromContext(context);
            var style = new Notification.BigTextStyle();
            style.BigText(message);

            int resourceId = Resource.Drawable.icon;

            var wearableExtender = new Notification.WearableExtender()
    .SetBackground(BitmapFactory.DecodeResource(context.Resources, resourceId))
                ;

            //Generate a notification with just short text and small icon
            var builder = new Notification.Builder(context)
                .SetPriority((int)NotificationPriority.Max)
                .SetVisibility(NotificationVisibility.Public)
                .SetDefaults(NotificationDefaults.All)
                .SetCategory(Notification.CategoryAlarm)
                .SetContentIntent(contentIntent)
                .SetSmallIcon(Resource.Drawable.icon)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
                .SetAutoCancel(true);

            var notification = builder.Build();
            manager.Notify(0, notification);
        }
}

AndroidReminderService.cs

public class AndroidReminderService : IReminderService
    {
        public void Remind(int seconds, string title, string message)
        {

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

            alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + seconds * 1000, pendingIntent);
        }

}

IReminderService.cs

public interface IReminderService
    {
        void Remind(int seconds, string title, string message);
    }

0 个答案:

没有答案