Eyn*_*ani 0 push-notification xamarin.android firebase xamarin.forms firebase-cloud-messaging
我正在使用 nuget 包Xamarin.Firebase.Messaging
并Xamarin.GooglePlayServices.Base
在我的应用程序中接收推送通知,以前它工作正常,但是当我更新时visual studio 2022 to 17.2.3
它停止工作
我尝试了所有这些:
<PackageReference Include="Xamarin.Google.Guava" ExcludeAssets="all"> <Version>27.1.0</Version> </PackageReference>
我之前所做的一切都不起作用
我的接收推送通知的代码:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我删除[Service]
或
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
代码编译正确
显然这是由于我对 Visual Studio 进行了更新,因为 android SDK 也更新了,解决方案是编辑[Services]
为[Services(Exported = true)]
android +31,留下这样的最终代码。
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
添加后,一切都正确编译
字体答案
归档时间: |
|
查看次数: |
1565 次 |
最近记录: |