Issue
In my xamarin.forms app I have used Firebase Push notifications. In the android portion I can receive notification on both Foreground ,background and killed state
. The problem I am facing is when I tap the notification in background state or killed state, I cant get the values from Intent
; It shows null. This was working perfectly earlier, I can't figure out what I have done wrong. I can get the values of notification when app is in foreground mode.
My 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());
}
}
Main Activity
namespace App.Droid
{
[Activity(Label = "App", Icon = "@mipmap/ic_launcher", Theme = "@style/MainTheme", MainLauncher = false ]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
static readonly string TAG = "MainActivity";
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
private bool isNotification = false;
protected override void OnCreate(Bundle savedInstanceState)
{
IsPlayServicesAvailable(); //You can use this method to check if play services are available.
CreateNotificationChannel();// Notification channel is required for Android 8.0 + to receive notifications.
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
// Here Iam getting Intent values as null on background or killed state
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
if (key != null)
{
var value = Intent.Extras.GetString(key);
string EmployeeID = Intent.Extras.GetString("EmpID");
string startDate = Intent.Extras.GetString("SDate");
string NotificationType = Intent.Extras.GetString("NotificationType");
string EmployeeName = Intent.Extras.GetString("EmployeeName");
string TotalHours = Intent.Extras.GetString("TotalHours");
string EmpNo = Intent.Extras.GetString("EmpNo");
LoadApplication(new App(true, EmployeeID, startDate, NotificationType, EmployeeName, TotalHours, EmpNo));
}
}
}
else
{ LoadApplication(new App(isNotification));
}
}
// <-------------- Notification click management in foregorund mode--->
protected override void OnNewIntent(Intent intent)
{
if (intent != null)
{
var message = intent.GetStringExtra("EmpID");
if (!string.IsNullOrEmpty(message))
{
string EmployeeID = intent.GetStringExtra("EmpID");
string startDate = intent.GetStringExtra("SDate");
string NotificationType = intent.GetStringExtra("NotificationType");
string EmployeeName = intent.GetStringExtra("EmployeeName");
string TotalHours = intent.GetStringExtra("TotalHours");
string EmpNo = intent.GetStringExtra("EmpNo");
LoadApplication(new App(true, EmployeeID, startDate, NotificationType, EmployeeName, TotalHours, EmpNo));
}
}
base.OnNewIntent(intent);
}
//<-------------- Checking whether google play service is availabe for fcm-------------------->
public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.Success)
{
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
{
}
// msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode);
else
{
//This device is not supported
Finish(); // Kill the activity if you want.
}
return false;
}
else
{
//Google Play Services is available.
return true;
}
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
{
Description = "Firebase Cloud Messages appear in this channel",
};
channel.EnableVibration(true);
channel.EnableLights(true);
channel.LockscreenVisibility = NotificationVisibility.Public;
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
}
I am using Xamarin.forms version 4.6.0.800
and Xamarin.Firebase.Messaging
version 71.1740.4
. Any help is appreciated
Edit
The issue was related to the SplashScreen I have. When I removed the SplashActivty, and set MainLauncher
is True
for MainActivity, the issue solved. I can get the Intent value even if App closed. So how can I solve the issue with Splashscreen ? Should I pass the Intent to MainActivty from SplashActivity?
My 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);
}
}
Solution
Sorry guys for this silly question. The issue was whenever I click notification the Intent will pass to SplashActivity. I didn't passed the Intent from Splash To Main Activity. Thanks @Cahyo for your help.
I added this in SplashActivity
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);
}
Answered By - Anand
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.