简体   繁体   中英

Getting a notification message to show my app in foreground if in background Android Xamarin

I apologize in advance as I am not a particularly experienced Android developer and I am working on an Android project in Xamarin using C#. I hope this question isn't a duplicate as I couldn't seem to find one yet, but if it is, please mark it as such and I'll gladly remove the question.

I want the icon for my droid app to show up in the notification bar when it is launched and while it's running. I want the icon and message removed when app goes into destroy event. So far I seem to have that down, but I can't seem to find or figure out how to make a click of the notification message bring my running app into foreground (only if it is not already). I think I'm taking the wrong general direction with my code for this and maybe this involves impending intents or something like that? Here's my code. Maybe I'm close or maybe I'm off in the wrong direction, but any assistance or pointers anyone can give would be greatly appreciated.

[Activity(Label = "MyActivity", MainLauncher = true, Theme = "@android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
    Notification notification = null;
    NotificationManager notificationManager = null;
    const int notificationId = 0;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Notification.Builder builder = new Notification.Builder(this)
                                           .SetContentTitle("My App is Running")
                                           .SetContentText("Show in forground")
                                           .SetSmallIcon(Resource.Drawable.Icon);

        // Build the notification:
        notification = builder.Build();

        // Get the notification manager:
        notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        // Publish the notification:
        notificationManager.Notify(notificationId, notification);
    }

    protected override void OnDestroy()
    {
        Log.Debug(logTag, "Location app is becoming inactive");
        notificationManager.Cancel(notificationId);
        base.OnDestroy();
    }
 }

I can't seem to find or figure out how to make a click of the notification message bring my running app into foreground (only if it is not already)

You need to say to the notification what it needs to do (which activity to start) when its clicked.

var intent = new Intent(context, typeof(MainActivity)); 
//activity will not be launched if it is already running at the top of the history stack.
intent.AddFlags(ActivityFlags.SingleTop);   

//Flag indicating that this PendingIntent can be used only once.
var pendingIntent = PendingIntent.GetActivity(context, 0
                                             , intent, PendingIntentFlags.OneShot);
Notification.Builder builder = new Notification.Builder(this)
                                           .SetContentTitle("My App is Running")
                                           .SetContentText("Show in forground")
                                           .SetSmallIcon(Resource.Drawable.Icon)
                                           .SetContentIntent(pendingIntent);

Read more about Notification.Builder to know what each and every item of the above code means and what other options you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM