Harsh Bakshi
Harsh Bakshi

Reputation: 55

Not able to recieve Remote Notifications in Xamarin.Android when the application is closed

I used this link for my app. https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/ // This is my GCM Listener Service using Android.App; using Android.Content; using Android.OS; using Android.Gms.Gcm; using Android.Util;

namespace Kites
{
[Service (Exported = false), IntentFilter (new [] { "com.google.android.c2dm.intent.RECEIVE" })]
public class MyGcmListenerService : GcmListenerService
{
    public override void OnMessageReceived (string from, Bundle data)
    {
        var message = data.GetString ("message");
        Log.Debug ("MyGcmListenerService", "From:    " + from);
        Log.Debug ("MyGcmListenerService", "Message: " + message);
        SendNotification (message);
    }

    void SendNotification (string message)
    {
        var intent = new Intent (this, typeof(Login));
        intent.AddFlags (ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Notification.Builder (this)
            .SetSmallIcon (Resource.Drawable.kiteslogo)
            .SetContentTitle ("GCM Message Test")
            .SetContentText (message)
            .SetAutoCancel (true)
            .SetDefaults (NotificationDefaults.Sound | NotificationDefaults.Vibrate)
            .SetContentIntent (pendingIntent);

        var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
        notificationManager.Notify (0, notificationBuilder.Build());
    }
}


}

This Is my Instance Id Listener Service using Android.App; using Android.Content; using Android.Gms.Gcm.Iid;

  namespace Kites
 {
[Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
class MyInstanceIDListenerService : InstanceIDListenerService
{
    public override void OnTokenRefresh()
    {
        var intent = new Intent (this, typeof (RegistrationIntentService));
        StartService (intent);
    }
}
}

This is My Registration Intent Service

 namespace Kites
  {
[Service(Exported = false)]
class RegistrationIntentService : IntentService
{
    static object locker = new object();

    public RegistrationIntentService() : base("RegistrationIntentService") { }

    protected override void OnHandleIntent (Intent intent)
    {
        try
        {
            Log.Info ("RegistrationIntentService", "Calling InstanceID.GetToken");
            lock (locker)
            {
                var instanceID = InstanceID.GetInstance (this);
                var token = instanceID.GetToken (
                    "SENDER_ID", GoogleCloudMessaging.InstanceIdScope, null);

                Log.Info ("RegistrationIntentService", "GCM Registration Token: " + token);
                SendRegistrationToAppServer (token);
                Subscribe (token);
            }
        }
        catch (Exception e)
        {
            Log.Debug("RegistrationIntentService", "Failed to get a registration token");
            return;
        }
    }

    void SendRegistrationToAppServer (string token)
    {
        // Add custom implementation here as needed.
    }

    void Subscribe (string token)
    {
        var pubSub = GcmPubSub.GetInstance(this);
        pubSub.Subscribe(token, "/topics/global", null);
    }
}
 }

This is the Message Sender Program i Use To Send Message { { class Program { public const string API_KEY =
"API KEY"; public const string MESSAGE = "MESSAGE";

    static void Main(string[] args)
    {
        var jGcmData = new JObject();
        var jData = new JObject();

        jData.Add("message", MESSAGE);
        jGcmData.Add("to", "/topics/global");
        jGcmData.Add("data", jData);

        var url = new Uri("https://gcm-http.googleapis.com/gcm/send");
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.TryAddWithoutValidation(
                    "Authorization", "key=" + API_KEY);

                Task.WaitAll(client.PostAsync(url,
                    new StringContent(jGcmData.ToString(), Encoding.Default, "application/json"))
                        .ContinueWith(response =>
                        {
                            Console.WriteLine(response);
                            Console.WriteLine("Message sent: check the client device notification tray.");
                        }));
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Unable to send GCM message:");
            Console.Error.WriteLine(e.StackTrace);
        }
    }
}
}

Sender Id And API KEY are corrrect but still im not able to recieve my notifications.

When i Try to send a message, in my messagesender.exe its confirmed that The message has been sent . Please check the device Tray. I Have followed everything correctly. Still i am not reciving any messages.

Upvotes: 1

Views: 827

Answers (1)

Jignesh Chudasama
Jignesh Chudasama

Reputation: 86

Check and update necessary following things for your code

  • Add Permission to manifest file.
  • debug and check coverage code.
  • recreate api key -- maybe you create it wrong

or You should use FCM link Firebase Cloud Messaging

Upvotes: 1

Related Questions