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?
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?
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.