gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:17:52 24 4
gpt4 key购买 nike

我将此链接用于我的应用程序。 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 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 key 正确,但我仍然无法收到通知。

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

最佳答案

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

  • 向 list 文件添加权限。
  • 调试和检查覆盖率代码。
  • 重新创建 api key ——也许你创建错误了

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

关于c# - 应用程序关闭时无法在 Xamarin.Android 中接收远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com