Firebase通知单击意图在后台状态显示空-Xamarin.forms Android
在我的xamarin.forms应用程序中,我使用了Firebase推送通知。在android部分,我可以接收前台、后台和终止状态的通知。我面临的问题是,当我在后台状态或终止状态下点击通知时,我无法从Firebase通知单击意图在后台状态显示空-Xamarin.forms Android,firebase,xamarin,xamarin.forms,xamarin.android,Firebase,Xamarin,Xamarin.forms,Xamarin.android,在我的xamarin.forms应用程序中,我使用了Firebase推送通知。在android部分,我可以接收前台、后台和终止状态的通知。我面临的问题是,当我在后台状态或终止状态下点击通知时,我无法从Intent获取值;它显示null。这在早些时候就很有效了,我不知道我做错了什么。我可以在应用程序处于前台模式时获取通知值 我的FirebaseMessagingService [Service] [IntentFilter(new[] { "com.google.firebase.MES
Intent
获取值;它显示null。这在早些时候就很有效了,我不知道我做错了什么。我可以在应用程序处于前台模式时获取通知值
我的FirebaseMessagingService
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
// private string TAG = "MyFirebaseMsgService";
public static string EmployeeID = "";
public static string StartDate = "";
public static string NotificationType = "";
public static string TotalHours = "";
public static string EmployeeName = "";
public static string EmployeeNumber = "";
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
try
{
SendNotification(message.GetNotification().Body, message.GetNotification().Title, message.Data);
EmployeeID = message.Data["EmpID"].ToString();
StartDate = message.Data["SDate"].ToString();
NotificationType = message.Data["NotificationType"].ToString();
TotalHours = message.Data["TotalHours"].ToString();
EmployeeName = message.Data["EmployeeName"].ToString();
EmployeeNumber = message.Data["EmpNo"].ToString();
}
catch (Exception ex)
{
}
}
private void SendNotification(string messageBody, string messageTitle, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("user_notification_id", EmployeeID);
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this, new Random().Next(), intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID).SetSmallIcon(Resource.Drawable.icon_logo).SetContentTitle(messageTitle).SetContentText(messageBody).SetAutoCancel(true).SetContentIntent(pendingIntent).SetVibrate(new long[] { 1000, 1000 }).SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)).SetStyle((new NotificationCompat.BigTextStyle().BigText(messageBody)));
var notificationManager = NotificationManagerCompat.From(this); notificationManager.Notify(new Random().Next(), notificationBuilder.Build());
}
}
首先,您需要在firebase负载中设置
“单击操作”:“打开活动1”
然后用IntentFilterAttribute
[IntentFilter(new[] { "OPEN_ACTIVITY_1" }, Categories = new[] { "android.intent.category.DEFAULT" })]
如果没有单击操作,您的通知将不知道要启动哪个活动
注意:
OPEN\u ACTIVITY\u 1
您可以更改此值,但您需要注意的是,Firebase
和IntentFilter
对于这个愚蠢的问题,抱歉各位。问题是每当我单击通知时,意图都会传递给SplashActivity。我没有将意图从飞溅传递到主要活动。谢谢@Cahyo的帮助
我在SplashActivity
[Activity(Label = "App", MainLauncher = true,
LaunchMode = LaunchMode.SingleTop,
ScreenOrientation = ScreenOrientation.Portrait,
Theme = "@style/splashscreen", NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.DecorView.SystemUiVisibility = StatusBarVisibility.Visible;
Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
}
InvokeMainActivity();
}
private void InvokeMainActivity()
{
var mainActivityIntent = new Intent(this, typeof(MainActivity));
mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
StartActivity(mainActivityIntent);
}
}
private void InvokeMainActivity()
{
var mainActivityIntent = new Intent(this, typeof(MainActivity));
if (Intent.Extras != null)
{
mainActivityIntent.PutExtras(Intent.Extras);
}
mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
StartActivity(mainActivityIntent);
}
我没有;如果之前通知负载中没有click_action属性,并且该属性已工作,是否还有其他原因?感谢您的指导。根据firebase通知文档,“如果必须激发不同的意图,则通知消息的点击动作字段必须设置为该意图(当未指定点击动作时使用启动器意图)。”因此,我的疑问是,如果我们不使用任何特定的点击动作,Intent.GetExtra是否将为空?
private void InvokeMainActivity()
{
var mainActivityIntent = new Intent(this, typeof(MainActivity));
if (Intent.Extras != null)
{
mainActivityIntent.PutExtras(Intent.Extras);
}
mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
StartActivity(mainActivityIntent);
}