这是我在这里的第一个问题,如果我的问题不合适,请提前抱歉!
我目前在MacBook上Xamarin
使用Visual Studio 2013
,我创建了一个PCL项目,我创建了所有必要的东西,使推送通知适用于iOS
和Android
。使用模拟器和真实设备我可以通过服务器端程序实现这两个程序,其中我使用从客户端创建的设备令牌。一切都运行良好几个小时,但过了一段时间,两个令牌都已过期。
我知道,因为这是我从服务器端给出的响应。
设备RegistrationId已过期:{e_bSR1SFBzg:APA91bHEQUEFvjn_mblu26MeKhAkcKRtXRLtaHOKYIsDTMDvJT3jmBDb4O6-hhu00ZhEcuAkpfaK-GKicbH1o5gtr3J9Sj9jyf-DVuikTd6DOBM0Tw70KFNcf3a2arMVLlHOy5QctQSn}
如果我从那个链接尝试它 http://1-dot-sigma-freedom-752.appspot.com/gcmpusher.jsp
为了让它再次起作用我从设备和模拟器中删除了应用程序,并在重新启动后创建了一个新的设备令牌,并且在几个小时的相同问题之后它工作了一段时间。 我的疑问是关于
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);
}
}
}
让我知道,理解和分析问题的代码是否足够,提前谢谢
[编辑] 我忘了添加&#39; MainActivity&#39;调用注册服务的文件:
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;
}
}
}
}
答案 0 :(得分:2)
我意识到让令牌过期并不是时间,但事实上,当你构建解决方案并转移应用程序时,它会进行一种卸载(保留旧数据)但是如果应用程序已经安装好了,GCM无法与“老人”合作。令牌。 我清理了设备上的数据并在真实设备中下载了应用程序,在模拟器中执行相同操作并发送通知后,两者都正常工作。 然后重新构建解决方案并仅在模拟器中下载;再次发送通知,但只有真实设备收到通知。
如果您正在进行调试,并且每次只想添加下面的代码,您都不想删除应用或数据。
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
答案 1 :(得分:0)
你有没有试过检查一下:
获取注册令牌
Android应用程序需要先注册GCM连接服务器才能接收消息。当应用程序注册时,它会收到注册令牌并将其发送到应用服务器。客户端应用程序应存储一个布尔值,指示是否已将注册令牌发送到服务器。
要使用此API,请在清单中包含InstanceIDListenerService
:
<service android:name="[.MyInstanceIDService]" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
此处是有关obtaining token的更多信息的链接。