【发布时间】:2020-02-25 03:05:14
【问题描述】:
我创建了一个 Xamarin.Forms 应用程序,但我面向的是 android 平台。我已经在 2 台设备上发布了该应用程序:三星(Android 6.0 API 23)和华为(Android 6.0 API 23),问题出在华为设备上,当我想创建一个本地通知时它需要在应用程序中断时显示,(通过创建广播接收器等)它不显示,否则在三星上它是,我确定问题不在代码上(很明显,代码不是专门针对一个特定的设备),所以我更相信问题出在我的华为设备上,对此问题有什么建议吗?
创建通知的代码
当用户点击“通知按钮”时调用的方法来调度重复的通知:
public static void EstablishNotification(long startSeconds)
=> alarmManager.SetRepeating(
AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + (startSeconds * 1000),
3600 * 1000, pendingIntent);
广播接收器绑定到警报管理器的意图
public override void OnReceive(Context context, Intent intent)
{
if (intent?.Extras != null)
{
title = intent.Extras.GetString(AndroidNotificationManager.TitleKey);
message = intent.Extras.GetString(AndroidNotificationManager.MessageKey);
}
var id = intent.Extras.GetInt(AndroidNotificationManager.ID);
Intent _intent = new Intent(AndroidApp.Context, typeof(SplashActivity));
_intent.PutExtra(TitleKey, title);
_intent.PutExtra(MessageKey, message);
_intent.PutExtra(ID, id);
PendingIntent pendingIntent = PendingIntent.GetActivity(
AndroidApp.Context,
id, _intent,
PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.SortexAppIcon))
.SetSmallIcon(Resource.Drawable.SortexAppIcon)
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);
manager =
(NotificationManager)context.GetSystemService(
AndroidApp.NotificationService);
var notification = builder.Build();
manager.Notify(messageId, notification);
【问题讨论】:
-
您创建了通知渠道吗?
-
@AndroDevil 是的
-
它是否可以在任何其他带有 android 8.0 的设备上运行?
-
@AndroDevil 我还没试过
-
请在其他安卓8.0的设备上查看。这样我们就可以隔离问题
标签: android xamarin.forms xamarin.android notifications huawei-mobile-services