应用程序关闭时无法在Xamarin.Android中收到远程通知

时间:2016-06-02 04:00:25

标签: c# android xamarin xamarin.android

我在我的应用中使用此链接。      https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/           //这是我的GCM侦听器服务       使用Android.App;      使用Android.Content;      使用Android.OS;     使用Android.Gms.Gcm;     使用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());
    }
}


}

这是我的实例ID侦听器服务     使用Android.App;     使用Android.Content;      使用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);
    }
}
}

这是我的注册意向服务

 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);
    }
}
 }

这是我用来发送消息的消息发送者程序     {                 {                    课程                    {                      public const string API_KEY =
                 " API KEY&#34 ;;                         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);
        }
    }
}
}

发件人ID和API密钥是正确的,但仍然无法接收我的通知。

当我尝试发送邮件时,在我的messagesender.exe中确认邮件已发送。请检查设备托盘。 我已经正确地遵循了一切。我仍然没有收到任何消息。

1 个答案:

答案 0 :(得分:1)

检查并更新代码所需的以下内容

  • 为清单文件添加权限。
  • 调试并检查覆盖代码。
  • 重新创建api密钥 - 也许你创建错误

或您应该使用FCM link Firebase云消息传递