我在我的xamarin.forms项目中使用xam.plugin.pushnotification
我的主要活动
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
//inicializa imageCircle
ImageCircleRenderer.Init();
//inicializa o mapa
global::Xamarin.FormsMaps.Init(this, bundle);
//shared Preferences
App.Init(new AndroidUserPreferences());
//Gerenciador de memória
CachedImageRenderer.Init();
try
{
AppContext = this.ApplicationContext;
CrossPushNotification.Initialize<CrossPushNotificationListener>("my sender");
StartPushService();
}
catch (Exception e)
{
var s = e.Message;
}
AndroidUserPreferences sharedPref = new AndroidUserPreferences();
if ( sharedPref.GetString("token") == " ")
{
GetTokenTask myTask = new GetTokenTask();
myTask.Execute(this);
}
LoadApplication(new App());
}
public static void StartPushService()
{
AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
public static void StopPushService()
{
AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
我的听众在我的pcl
中public class CrossPushNotificationListener : IPushNotificationListener
{
public void OnMessage(JObject values, DeviceType deviceType)
{
Debug.WriteLine("Message Arrived");
}
public void OnRegistered(string token, DeviceType deviceType)
{
Debug.WriteLine(string.Format("Push Notification - Device Registered - Token : {0}", token));
}
public void OnUnregistered(DeviceType deviceType)
{
Debug.WriteLine("Push Notification - Device Unnregistered");
}
public void OnError(string message, DeviceType deviceType)
{
Debug.WriteLine(string.Format("Push notification error - {0}",message));
}
public bool ShouldShowNotification()
{
return true;
}
}
}
在app.cs(PCL)中注册(尝试LOL)
public App()
{
InitializeComponent();
CrossPushNotification.Current.Register();
MainPage = new NavigationPage(new Views.Splash2());
}
我使用软件包名称在firebase中注册了我的项目,然后我在那里创建了一个项目并获取了发件人ID ...但是...... 在电话&#34;交叉... current.register()&#34;,某处(它没有告诉我在哪里),我有一个例外
FATAL UNHANDLED EXCEPTION:System.TypeLoadException:无法使用标记0100005a解析类型(来自typeref,类/程序集Android.Gms.Gcm.Iid.InstanceID,Xamarin.GooglePlayServices.Gcm,Version = 1.0.0.0,Culture = neutral ,PublicKeyToken = null)
我需要在我的pcl项目中安装xamarin.gcm吗?现在它只在我的android项目中
答案 0 :(得分:0)
尝试将CrossPushNotification.Current.Register ();
调用为OnCreate
方法。像这样:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
//inicializa imageCircle
ImageCircleRenderer.Init();
//inicializa o mapa
global::Xamarin.FormsMaps.Init(this, bundle);
//shared Preferences
App.Init(new AndroidUserPreferences());
//Gerenciador de memória
CachedImageRenderer.Init();
try
{
AppContext = this.ApplicationContext;
CrossPushNotification.Initialize<CrossPushNotificationListener>("my sender");
//call register method here
CrossPushNotification.Current.Register();
StartPushService();
}
catch (Exception e)
{
var s = e.Message;
}
AndroidUserPreferences sharedPref = new AndroidUserPreferences();
if ( sharedPref.GetString("token") == " ")
{
GetTokenTask myTask = new GetTokenTask();
myTask.Execute(this);
}
LoadApplication(new App());
}
答案 1 :(得分:0)
我也必须迁移到1.2.5-beta,它确实有效,但该插件最近宣布为DEPRECATED。
这是个坏消息。
为了支持monoandroid80,我不得不分叉xam.plugin.pushnotification并手动更新其packages.config。
除了迁移之外,别无选择。