Issue
This is my fisrt time in posting so if I have missed anything out please let me know.
I am developing a foreground service which is all working fine. My question is how do I launch the application from the foreground service notification?
One the application is closed the foreground service is still running. Is there a way where I can allow the user to pull down the drawer and tap the running service and launch the application?
I hope someone can help. This is my code for the service.
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;
}
Solution
The notification.SetContentIntent method is the specific way to set the action you want to do when user taps the running service and the PendingIntent.GetActivity method should get an activity. Such as:
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()" } });
}
}
Answered By - Liyun Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.