C# 本地推送通知仅在调试模式下有效Xamarin Forms Android
我在Xamarin表单中工作,并设置了一个接口,然后调用iOS和Android。在iOS上,我没有遇到任何问题,但我在Android上遇到了一些问题 现在,在调试模式下工作得很好,但是一旦我在发布模式下测试它,我就不会收到通知声音,即使我已经确认事件正在被调用 这是我的android特定代码:C# 本地推送通知仅在调试模式下有效Xamarin Forms Android,c#,android,xamarin.forms,xamarin.android,C#,Android,Xamarin.forms,Xamarin.android,我在Xamarin表单中工作,并设置了一个接口,然后调用iOS和Android。在iOS上,我没有遇到任何问题,但我在Android上遇到了一些问题 现在,在调试模式下工作得很好,但是一旦我在发布模式下测试它,我就不会收到通知声音,即使我已经确认事件正在被调用 这是我的android特定代码: [assembly: Dependency(typeof(Push_Droid))] namespace Project.Droid.DI { public class Push_Droid : IPus
[assembly: Dependency(typeof(Push_Droid))]
namespace Project.Droid.DI
{
public class Push_Droid : IPush
{
NotificationManager notificationManager;
Notification notification;
internal static readonly string CHANNEL_ID = "my_notification_channel";
NotificationChannel channel;
private int IDCount = 0;
public async Task HandleNewNotification(string User, string Text, string Image)
{
var context = Xamarin.Forms.Forms.Context;
Intent intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var notificationBuilder = new NotificationCompat.Builder(context)
.SetContentTitle(User)
.SetSmallIcon(Resource.Drawable.play)
.SetContentText(Text)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.SetContentTitle(User)
.SetSmallIcon(Resource.Drawable.play)
.SetContentText(Text)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(IDCount, notificationBuilder.Build());
IDCount++;
}
}
}
[程序集:依赖项(typeof(Push_Droid))]
命名空间Project.Droid.DI
{
公共级推送机器人:IPush
{
通知经理通知经理;
通知;
内部静态只读字符串通道\u ID=“我的通知\u通道”;
通知频道;
私有整数IDCount=0;
公共异步任务HandleNewNotification(字符串用户、字符串文本、字符串图像)
{
var context=Xamarin.Forms.Forms.context;
意向意向=新意向(上下文、类型(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent=pendingIntent.GetActivity(上下文,0,意图,PendingIntentFlags.OneShot);
if(Build.VERSION.SdkInt
我是否缺少权限或类似的内容?我没有在清单中添加任何与此功能相关的内容-但我关注的是
accessnotificationpolicy
,但当我读到它时,它似乎并不能解决我的问题。我认为本地通知的发布模式中没有其他设置。请阅读并尝试一下,看看它是否在发布模式下工作。我使用了他们的代码,现在它可以工作了!谢谢您的建议Jack@DiddanDo你能指出他们的代码和你的代码之间的主要区别,并将其作为这个问题的答案发布吗?这可能会帮助其他人在将来遇到你的问题时结结巴巴。我认为没有广告本地通知的发布模式下的附加设置。请阅读并试用,以查看它是否在发布模式下工作。我使用了他们的代码,现在它可以工作了!建议使用ThxJack@DiddanDo你能指出他们的代码和你的代码之间的主要区别并将其作为这个问题的答案发布吗?这可能会帮助其他人将来在你的问题上结结巴巴。