2

I'm building an app in android using Xamarin.forms.

Now I have implemented GCM Services to get notification. And here I want to open a page in my application when user clicks on the notification.

How can I achieve this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hetal
  • 631
  • 1
  • 7
  • 19
  • I have trried to create new activitty when user notification but since xamarin only support single activity it's not possible and gives me error – Hetal Oct 19 '15 at 11:36

1 Answers1

3

In your GCMService I have this. I send the customParam as part of the notification, that helps you differentiate it from other notifications.

protected override void OnMessage(Context context, Intent intent) { Log.Info(Tag, "GCM Message Received!");

var message = intent.Extras.Get("msg").ToString();
var customParam = "";

if (intent.Extras.ContainsKey("customParam"))
{
    customParam = intent.Extras.Get("customParam").ToString();
}

// This is a custom class I use to track if the app is in the foreground or background
if (Platform.StatusTracker.InView)
{
    // In foreground, hence take over and show my internal toast notification instead
    // Show Toast
}
else
{
    CreateNotification("", message, customParam);
}

}

private void CreateNotification(string title, string desc, string customParam) { // Create notification var notificationManager = GetSystemService(NotificationService) as NotificationManager;

// Create an intent to show UI
var uiIntent = new Intent(this, typeof(MainActivity));

uiIntent.PutExtra("customParam", customParam);

// Create the notification
var builder = new NotificationCompat.Builder(this);
 Notification notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
.SetSmallIcon(Android.Resource.Drawable.SymActionEmail).SetTicker(desc)
.SetAutoCancel(true).SetContentTitle(title)
.SetContentText(desc).Build();

// Auto cancel will remove the notification once the user touches it
notification.Flags = NotificationFlags.AutoCancel;

// Show the notification
if (notificationManager != null) notificationManager.Notify(1, notification);

}

In your MainActivity.cs add in this

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (data.HasExtra("customParam"))
    {
        var customParam = data.GetStringExtra("customParam");
        if (!String.IsNullOrEmpty(customParam))
        {
            data.RemoveExtra("customParam");
            // Do your navigation or other functions here
         }
     }
}

And based on the customParam you can move to the navigation page of your choice. Since you are using forms, use your Dependency Injected navigation service to handle that for you.

Adam
  • 16,089
  • 6
  • 66
  • 109
  • Hey i'm beginner to xamarin, can you please elaborate what is "Dependency Injected navigation service" ? – Hetal Oct 19 '15 at 05:58
  • Normally you have a NavigationPage and you go Navigation.PushAsync() to go to another page. However as you are in Android and Forms is all loaded in a single Activity, you need to get to that NavigationPage to push your new page. This shows how dependency injection works: http://forums.xamarin.com/discussion/17368/dependency-injection You then can create a NavigationService Class with an Interface of INavigationService and call upon that to move pages. Personally I use MVVMLight and this is how they do it: https://marcominerva.wordpress.com/2014/10/10/navigationservice-in-mvvm-light-v5/ – Adam Oct 19 '15 at 06:05
  • What are you experiencing ? – Adam Oct 19 '15 at 08:48
  • Now i understood when application is running then in that case we need to show toast to inform user about notification and if application is in background or close then only we need to show notification to user. is this correct – Hetal Oct 22 '15 at 08:52
  • I think so from what I understand you are saying. When the app is in the background the notification will only go in the relevant notification center on each platform. Users can then click that to open the app. If the app is open you need to show them the toast in the app (if applicable). – Adam Oct 22 '15 at 09:25
  • Thanks. Marking dis as ans. – Hetal Oct 22 '15 at 12:35