我使用nuget包Xamarin.Firebase.Messaging
和Xamarin.GooglePlayServices.Base
在我的应用程序中接收推送通知,以前它工作正常,但当我更新visual studio 2022 to 17.2.3
时它停止工作
我试了所有这些:
- 更新所有nuget包
- 从所有共享项目中删除obj bin文件夹
- 使multidex
- 安装并包含
<PackageReference Include="Xamarin.Google.Guava" ExcludeAssets="all"> <Version>27.1.0</Version> </PackageReference>
- https://github.com/xamarin/GooglePlayServicesComponents/issues/379
我之前做的都不起作用
我接收推送通知的代码:
using System;
using System.Threading.Tasks;
using Android.App;
using Firebase.Messaging;
using Plugin.DeviceInfo;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MyApp.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
readonly AndroidNotificationManager _androidNotification = new AndroidNotificationManager();
public override void OnMessageReceived(RemoteMessage message)
{
var mensajeData = message.Data;
string title= mensajeData["notiTitle"];
string bodymessage= mensajeData["notiBody"];
_androidNotification.CreateLocalNotification(title, bodymessage);
}
public override void OnNewToken(string token)
{
base.OnNewToken(token);
Preferences.Set("TokenFirebase", token);
}
}
}
如果我删除[Service]
或[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
代码正确编译
显然这是由于我对visual studio的更新,因为android SDK也更新了,解决方案是将android +31的[Services]
编辑为[Services(Exported = true)]
,留下这样的最终代码。
[Service(Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
readonly AndroidNotificationManager _androidNotification = new AndroidNotificationManager();
public override void OnMessageReceived(RemoteMessage message)
{
var mensajeData = message.Data;
string title= mensajeData["notiTitle"];
string bodymessage= mensajeData["notiBody"];
_androidNotification.CreateLocalNotification(title, bodymessage);
}
public override void OnNewToken(string token)
{
base.OnNewToken(token);
Preferences.Set("TokenFirebase", token);
}
}
添加后,一切都正确编译
字体的回答
除了这里的答案,我还必须导出广播回复器
[BroadcastReceiver(Enabled = true, Exported = true)]