从 Firebase 通知恢复应用程序不起作用(Xamarin Forms)
Resume App from a Firebase Notification is not working (Xamarin Forms)
我正在努力将 firebase 推送通知 集成到我的应用程序中。
请找到我的 firebase FirebaseMessagingService class.
如果应用程序已打开并且 运行 一切正常。但是如果应用程序未打开/如果我切换到其他应用程序(我的应用程序未关闭)。我收到通知,但当我点击“通知”时,它会重新启动应用程序而不恢复。
我在主 activity 中使用启动模式 LaunchMode = LaunchMode.SingleTop。
如果应用程序打开,我会在 OnNewIntent 主 activity 覆盖方法中得到响应。
谁能帮我弄清楚真正的原因。请帮忙。
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class DriverAppMessagingService : FirebaseMessagingService
{
#region Overriden Methods
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
var parameters = new Dictionary<string, object>();
var notification = message.GetNotification();
if (null != notification)
{
if (!string.IsNullOrEmpty(notification.Body))
{
parameters.Add("Body", notification.Body);
}
if (!string.IsNullOrEmpty(notification.BodyLocalizationKey))
{
parameters.Add("BodyLocalizationKey", notification.BodyLocalizationKey);
}
// convert the incoming message to a local notification
SendLocalNotification(parameters);
// send the incoming message directly to the MainActivty
SendNotificationToMainActivity(parameters);
}
}
public override void OnNewToken(string p0)
{
base.OnNewToken(p0);
//Persist the token to app settings for registration purpose.
AppDefinition.Helpers.Settings.Current.PnsHandle = p0;
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="args"></param>
private void SendNotificationToMainActivity(Dictionary<string, object> args)
{
if (CrossCurrentActivity.Current.Activity is MainActivity activity)
{
var message = args["Body"].ToString();
activity.TriggerPushNotification(message);
}
}
/// <summary>
/// Method to trigger the local notification.
/// </summary>
/// <param name="args"></param>
private void SendLocalNotification(Dictionary<string, object> args)
{
//TODO Only using one token from message response.
var message = args["Body"].ToString();
var intent = new Intent(CrossCurrentActivity.Current.Activity, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("message", message);
var pendingIntent = PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(CrossCurrentActivity.Current.Activity, Constants.NotificationChannelName)
.SetContentTitle(Constants.ContentTitle)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
.SetContentText(message)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetLights(0xff0000, 100, 100)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(Constants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(CrossCurrentActivity.Current.Activity);
notificationManager.Notify(0, notificationBuilder.Build());
}
#endregion
}
当您设置 MainActivity LaunchMode = LaunchMode.SingleTop
并设置 ActivityFlags.ClearTop
时,当您点击通知打开您的应用程序时,它将清除 MainActivity 之上的所有活动并将 MainActivity 放在堆栈的顶部。
而不是重新创建MainActivity,而是进入OnNewIntent
方法
可以在OnCreate
方法中打断点,点击通知后打开应用程序时,不会进入。
数据消息 - 由客户端应用处理。即使您的应用程序处于 foreground/background/killed,这些消息也会触发 onMessageReceived() 回调。使用此类消息时,您是提供 UI 并在 Android 设备上收到推送通知时处理的人。
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"priority": "high",
"condition": "'general' in topics"
}
试试这个,这将解决您的问题。
我正在努力将 firebase 推送通知 集成到我的应用程序中。
请找到我的 firebase FirebaseMessagingService class.
如果应用程序已打开并且 运行 一切正常。但是如果应用程序未打开/如果我切换到其他应用程序(我的应用程序未关闭)。我收到通知,但当我点击“通知”时,它会重新启动应用程序而不恢复。
我在主 activity 中使用启动模式 LaunchMode = LaunchMode.SingleTop。
如果应用程序打开,我会在 OnNewIntent 主 activity 覆盖方法中得到响应。
谁能帮我弄清楚真正的原因。请帮忙。
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class DriverAppMessagingService : FirebaseMessagingService
{
#region Overriden Methods
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
var parameters = new Dictionary<string, object>();
var notification = message.GetNotification();
if (null != notification)
{
if (!string.IsNullOrEmpty(notification.Body))
{
parameters.Add("Body", notification.Body);
}
if (!string.IsNullOrEmpty(notification.BodyLocalizationKey))
{
parameters.Add("BodyLocalizationKey", notification.BodyLocalizationKey);
}
// convert the incoming message to a local notification
SendLocalNotification(parameters);
// send the incoming message directly to the MainActivty
SendNotificationToMainActivity(parameters);
}
}
public override void OnNewToken(string p0)
{
base.OnNewToken(p0);
//Persist the token to app settings for registration purpose.
AppDefinition.Helpers.Settings.Current.PnsHandle = p0;
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="args"></param>
private void SendNotificationToMainActivity(Dictionary<string, object> args)
{
if (CrossCurrentActivity.Current.Activity is MainActivity activity)
{
var message = args["Body"].ToString();
activity.TriggerPushNotification(message);
}
}
/// <summary>
/// Method to trigger the local notification.
/// </summary>
/// <param name="args"></param>
private void SendLocalNotification(Dictionary<string, object> args)
{
//TODO Only using one token from message response.
var message = args["Body"].ToString();
var intent = new Intent(CrossCurrentActivity.Current.Activity, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("message", message);
var pendingIntent = PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(CrossCurrentActivity.Current.Activity, Constants.NotificationChannelName)
.SetContentTitle(Constants.ContentTitle)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
.SetContentText(message)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetLights(0xff0000, 100, 100)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(Constants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(CrossCurrentActivity.Current.Activity);
notificationManager.Notify(0, notificationBuilder.Build());
}
#endregion
}
当您设置 MainActivity LaunchMode = LaunchMode.SingleTop
并设置 ActivityFlags.ClearTop
时,当您点击通知打开您的应用程序时,它将清除 MainActivity 之上的所有活动并将 MainActivity 放在堆栈的顶部。
而不是重新创建MainActivity,而是进入OnNewIntent
方法
可以在OnCreate
方法中打断点,点击通知后打开应用程序时,不会进入。
数据消息 - 由客户端应用处理。即使您的应用程序处于 foreground/background/killed,这些消息也会触发 onMessageReceived() 回调。使用此类消息时,您是提供 UI 并在 Android 设备上收到推送通知时处理的人。
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"priority": "high",
"condition": "'general' in topics"
}
试试这个,这将解决您的问题。