В настоящее время я использую сообщения Firebase для своего push-уведомления.Я присоединяю данные к своему намерению, чтобы я мог их захватить, и мое приложение может использовать эти данные при нажатии кнопки.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void HandleIntent(Intent intent)
{
CreateNotification(intent);
}
private void CreateNotification(Object e)
{
var i = e as Intent;
var bundle = i.Extras;
var intent = new Intent(this, typeof(MainActivity));
var notificationName = bundle.GetString("notificationName");
if (!string.IsNullOrEmpty(notificationName))
{
intent.PutExtra("notificationName", notificationName);
}
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.icon_notification);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon));
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
builder.SetChannelId("YourChannelID");
}
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Затем на моей стороне MainActivity в программе запускается OnCreate, когда приложениезапущен обратно на передний план.
protected override void OnCreate(Bundle bundle)
{
....
App.NotificationName = Intent.GetStringExtra("notificationName");
....
}
Проблема, с которой я сталкиваюсь сейчас, заключается в том, что одно из моих устройств (ОС Android 7) работает как положено, OnCreate запускается, когда приложение выводится на передний план.Проблема, с которой я столкнулся сейчас, заключается в том, что OnCreate не запускается на другом устройстве (ОС Android 8).Каков наилучший способ справиться с этими намерениями, и где я должен поместить этот код App.NotificationName = Intent.GetStringExtra("notificationName");
, чтобы он срабатывал на любом устройстве