gpt4 book ai didi

c# - 如何刷新一段时间后过期的 GCM 中的注册 token ?

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:49 25 4
gpt4 key购买 nike

这是我在这里的第一个问题,如果我的问题中有什么不对,请提前道歉!

我目前在 MacBook 上的 Visual Studio 2013 中使用 Xamarin,我创建了一个 PCL 项目,并创建了使推送通知工作所需的所有内容iOSAndroid。使用模拟器和真实设备,我可以通过服务器端程序实现这两者,我使用从客户端创建的设备 token 。几个小时内一切正常,但过了一会儿,两个 token 都过期了。我知道,因为这是我从服务器端给出的响应。

设备注册 ID 已过期:{e_bSR1SFBzg:APA91bHEQUEFvjn_mblu26MeKhAkcKRtXRLtaHOKYIsDTMDvJT3jmBDb4O6-hhu00ZhEcuAkpfaK-GKicbH1o5gtr3J9Sj9jyf-DVuikTd6DOBM0Tw70KFNVcLcL}

如果我从那个链接尝试 http://1-dot-sigma-freedom-752.appspot.com/gcmpusher.jsp

为了让它再次运行,我从设备和模拟器中删除了该应用程序,并在重新启动后创建了一个新的设备 token ,它运行了一段时间,但在几个小时后出现了同样的问题。我的疑问是关于

InstanceIDListenerService

可能没有正确配置的文件。下面是我的部分代码,所以如果有人可以给我任何建议,因为最近两天我一直面临这个问题,我不知道该怎么做。

我的 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="MY.PACKAGE.NAME" android:installLocation="auto" android:versionCode="1" android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="MY.PACKAGE.NAME.permission.C2D_MESSAGE" />
<permission android:name="MY.PACKAGE.NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<!--<application android:label="TruckMe" android:icon="@drawable/ic_launcher">-->
<application android:label="TruckMe" android:icon="@drawable/ic_launcher">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="KEY_FOR-MAPS" />
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="MY.PACKAGE.NAME" />
</intent-filter>
</receiver>
</application>
</manifest>

当然,我用那个词替换了我的真实包名称,Api_KEY 也用同样的东西替换。RegistrationIntentService.cs 文件:

using System;
using Android.App;
using Android.Content;
using Android.Util;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;


namespace TruckMe.Droid.Infrastructure.Notifications
{
[Service(Exported = false)]
public class RegistrationIntentService : IntentService
{
private const string SENDER_ID = "my_sender_num";
static object locker = new object();

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

protected override void OnHandleIntent(Intent intent)
{
try
{
Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken");
lock (locker)
{
// Initially this call goes out to the network to retrieve the token, subsequent calls are local.
var instanceID = InstanceID.GetInstance(this);

// See https://developers.google.com/cloud-messaging/android/start for details on this file.
var token = instanceID.GetToken(SENDER_ID, GoogleCloudMessaging.InstanceIdScope, null);

//Log.Info("RegistrationIntentService", "GCM Registration Token: " + token);
Console.WriteLine("RegistrationIntentService - GCM Token: " + token);
SendRegistrationToAppServer(token);
Subscribe(token);
//unSubscribe(token);
}
}
catch (Exception ex)
{
Log.Debug("RegistrationIntentService", "Failed to get a registration token. Ex: " + ex.Message);
return;
}
}

void SendRegistrationToAppServer(string token)
{
// Add custom implementation here as needed.
//Console.WriteLine("RegistrationIntentService - GCM Token: " + token);

// save the value to send it to our server to manage the notifications
App.UserPreferences.SetString("DeviceToken", token);
}

void Subscribe(string token)
{
var pubSub = GcmPubSub.GetInstance(this);
// subscribe to a topic group
pubSub.Subscribe(token, "/topics/global", null); // working

}

}
}

然后是 InstanceIDListenerService 文件:

using Android.App;
using Android.Content;
using Android.Gms.Gcm.Iid;


namespace TruckMe.Droid.Infrastructure.Notifications
{
[Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
public class MyInstanceIdListenerService : InstanceIDListenerService
{
/**
* Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised.
* This call is initiated by the InstanceID provider.
*/
public override void OnTokenRefresh()
{
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
var intent = new Intent(this, typeof(RegistrationIntentService));
StartService(intent);
}
}
}

让我知道理解和分析问题的代码是否足够,提前致谢

[编辑]我也忘了添加调用注册服务的“MainActivity”文件:

using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Gms.Common;
using Android.Graphics.Drawables;
using Android.Views;
using Android.OS;
using Xamarin;
using TruckMe.Droid.Infrastructure;
using TruckMe.Droid.Infrastructure.Notifications;


namespace TruckMe.Droid
{
[Activity(Label = "TruckMe",
MainLauncher = false,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
WindowSoftInputMode = SoftInput.AdjustPan)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

if (IsPlayServicesAvailable())
{
var intent = new Intent(this, typeof(RegistrationIntentService));
StartService(intent);
}

global::Xamarin.Forms.Forms.Init(this, bundle);

FormsMaps.Init(this, bundle);

//Forms.SetTitleBarVisibility(AndroidTitleBarVisibility.Never);
LoadApplication(new App());

// below the code to remove the icon from android navigation bar
ActionBar.SetIcon(new ColorDrawable(Android.Graphics.Color.Transparent));

App.Init(new AndroidUserPreferences());
}


public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.Success)
{
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
Console.WriteLine(GoogleApiAvailability.Instance.GetErrorString(resultCode));
else
{
Console.WriteLine("Sorry, this device is not supported");
Finish();
}
return false;
}
else
{
Console.WriteLine("Google Play Services is available.");
return true;
}
}

}
}

最佳答案

我意识到让 token 过期不是时间问题,而是当您构建解决方案并传输应用程序时,它会进行某种卸载(保留旧数据),但如果应用程序已安装 GCM 不适用于“旧” token 。我清理了设备上的数据并在真实设备上下载了应用程序,在模拟器上做了同样的事情并发送了通知,两者都在工作。然后只需重新构建解决方案并仅在模拟器中下载;再次发送通知,但只有真实设备收到。

如果您正在调试并且不想每次都删除应用或数据,只需在下面调用注册服务的地方添加代码即可。

    var instanceID = InstanceID.GetInstance(this);

// See https://developers.google.com/cloud-messaging/android/start for details on this file.
var token = instanceID.GetToken(SENDER_ID, GoogleCloudMessaging.InstanceIdScope, null);

#if DEBUG
instanceID.DeleteToken(token, GoogleCloudMessaging.InstanceIdScope);
instanceID.DeleteInstanceID();
#endif

token = instanceID.GetToken(SENDER_ID, GoogleCloudMessaging.InstanceIdScope, null);

这部分代码是从以下链接向我推荐的: fromXamarinForum

关于c# - 如何刷新一段时间后过期的 GCM 中的注册 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737850/

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