[英]Xamarin Android Launch Application from Foreground Notification
这是我第一次发帖,所以如果我错过了什么,请告诉我。
我正在开发一个一切正常的前台服务。 我的问题是如何从前台服务通知启动应用程序?
一是应用程序关闭,前台服务仍在运行。 有没有办法让用户拉下抽屉并点击正在运行的服务并启动应用程序?
我希望有人能帮帮忙。 这是我的服务代码。
private void startForegroundService()
{
try
{
var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var intent = new Intent(this, typeof(ProteoForegroundService));
PendingIntent pendingIntent = PendingIntent.GetActivity(this, PENDING_INTENT_ID, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
createNotificationChannel(notifcationManager);
}
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notification.SetAutoCancel(false);
notification.SetOngoing(true);
notification.SetSmallIcon(Resource.Drawable.Icon);
notification.SetContentTitle("Proteo Mobile");
notification.SetContentText("Proteo Mobile is running");
notification.SetContentIntent(pendingIntent);
StartForeground(NOTIFICATION_ID, notification.Build());
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "startForegroundService()" } });
}
}
private void createNotificationChannel(NotificationManager notificationMnaManager)
{
try
{
var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
NotificationImportance.Low);
notificationMnaManager.CreateNotificationChannel(channel);
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "createNotificationChannel()" } });
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
startForegroundService();
return StartCommandResult.NotSticky;
}
notification.SetContentIntent 方法是设置用户点击正在运行的服务时要执行的操作的具体方法,而 PendingIntent.GetActivity 方法应该获得一个活动。 如:
private void startForegroundService()
{
try
{
var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var intent = new Intent(this, typeof(MainActivity));
PendingIntent pendingIntent = PendingIntent.GetActivity(this, PENDING_INTENT_ID, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
createNotificationChannel(notifcationManager);
}
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notification.SetAutoCancel(false);
notification.SetOngoing(true);
notification.SetSmallIcon(Resource.Drawable.Icon);
notification.SetContentTitle("Proteo Mobile");
notification.SetContentText("Proteo Mobile is running");
notification.SetContentIntent(pendingIntent);
StartForeground(NOTIFICATION_ID, notification.Build());
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "startForegroundService()" } });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.