【问题标题】:Not able to recieve Remote Notifications in Xamarin.Android when the application is closed应用程序关闭时无法在 Xamarin.Android 中接收远程通知
【发布时间】:2016-10-01 15:12:35
【问题描述】:

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

这是我用来发送消息的消息发送程序 { { 课堂节目 { 公共常量字符串 API_KEY =
“API 密钥”; 公共常量字符串 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 和 API KEY 正确,但我仍然无法收到我的通知。

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

【问题讨论】:

  • 您是否按照程序的流程检查是否有任何调用失败?检查您是否获得了实例ID,检查您是否正确地将注册发送到您的服务器,并检查服务器是否在收到ID后正确发送了消息。还要记得将相关权限添加到 android 清单中。
  • 是的,一切都已正确完成。我已经检查过了。但是我仍然无法接收消息。甚至添加了 Manifest 的权限。
  • 尝试以发布模式部署您的应用程序。
  • @BhavinSurelia 成功了。谢谢

标签: c# android xamarin xamarin.android


【解决方案1】:

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

  • 向清单文件添加权限。
  • 调试和检查覆盖率代码。
  • 重新创建 api 密钥 -- 可能你创建错了

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

【讨论】:

  • 是的。我的 android manifest 有问题。它帮助了我。
  • 但是当我从我的多任务应用程序管理器中关闭我的应用程序时,我没有收到任何通知。我需要一些帮助
  • 1.你用的是设备还是模拟器? 2. 检查服务器连接可能你的服务器没有收到请求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
相关资源
最近更新 更多