Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

How to open intent on click push notifications.

$
0
0

I'm having some trouble understanding how the pushnotifications should work.
When the user clicks on the notification dissapears but the activity does not start.

I have a specific GCM module which is an Android Library. This is where all my notification logic is.

This module does not have access to my MainActivity directly, the Droid project includes this library.

At the start of my MainActivity I initialize the GCM module.

In the module this is how I build my notification

 [Service]
    public class MyIntentService : IntentService
    {
         ...

  protected override void OnHandleIntent(Intent intent)
        {
        try
        {
            Context context = this.ApplicationContext;
            string action = intent.Action;

            if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
            {
                NotificationBuilder(intent);

            }
        }
        finally
        {
            lock (LOCK)
            {
                //Sanity check for null as this is a public method
                if (sWakeLock != null)
                    sWakeLock.Release();
            }
        }
    }

public void NotificationBuilder( Intent intent)
    {
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            Notification.Builder builder = new Notification.Builder(this);
            try
            {
                string text = !System.String.IsNullOrWhiteSpace(intent.GetStringExtra("title")) ?
                                             intent.GetStringExtra("title").ToString() : "Data not available";
                string message = !System.String.IsNullOrWhiteSpace(intent.GetStringExtra("message")) ?
                                                 intent.GetStringExtra("message").ToString() : "Data not available";

                builder.SetContentTitle(text);
                builder.SetSmallIcon(Resource.Drawable.icon);
                builder.SetContentText(message).SetDefaults(0);
                builder.SetContentIntent(pendingIntent);
                builder.SetAutoCancel(true); 
                //builder.SetDefaults(NotificationDefaults.All);

                // Obtain a reference to the NotificationManager
                NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                Notification notification = builder.Build();
                notificationManager.Notify(3, notification);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error handling notification:" + e.Message);
                Xamarin.Insights.Report(e, Xamarin.Insights.Severity.Warning);
            }
    }

Any idea is welcome


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>