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

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

我将此 link 用于我的应用程序。 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);
    }
}
 }

这是我用来发送消息的消息发送程序 { { class 程序 { public 常量字符串 API_KEY =
"API KEY"; public 常量字符串消息 = "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 中确认消息已发送。请检查设备托盘。 我已经正确地遵循了一切。我仍然没有收到任何消息。

为您的代码检查并更新必要的以下内容

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

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