Xamarin.android 推送通知未到达
我正在使用Xamarin开发一个android应用程序,对于推送通知,我正在使用PushSharp。 我在应用程序未运行时(例如,重新启动后)接收推送通知时遇到一些问题。以下是服务代码:Xamarin.android 推送通知未到达,xamarin.android,xamarin,pushsharp,Xamarin.android,Xamarin,Pushsharp,我正在使用Xamarin开发一个android应用程序,对于推送通知,我正在使用PushSharp。 我在应用程序未运行时(例如,重新启动后)接收推送通知时遇到一些问题。以下是服务代码: [BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)] [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE }, Categories = ne
[BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)]
[IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "com.xxx" })]
[IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "com.xxx" })]
[IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "com.xxx" })]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class PushHandlerBroadcastReceiver : PushHandlerBroadcastReceiverBase<PushHandlerService>
{
//IMPORTANT: Change this to your own Sender ID!
//The SENDER_ID is your Google API Console App Project ID.
// Be sure to get the right Project ID from your Google APIs Console. It's not the named project ID that appears in the Overview,
// but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
// where 785671162406 is the project id, which is the SENDER_ID to use!
public static string[] SENDER_IDS = new string[] {"1234"};
public const string TAG = "PushSharp-GCM";
}
我错过什么了吗?手机重新启动后,为什么服务没有启动。我应该在广播接收器里做些什么吗?我是否应该注册到服务构造函数中的推送通知(以处理应用程序尚未启动的情况)?如果您的服务在重新启动时未启动,则可以向启动它的项目添加一个
广播接收器
:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class MyBootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
MyNotificationService.RunIntentInService(context, intent);
SetResult(Result.Ok, null, null);
}
}
如果您使用的是
PushSharp
,您可能不需要将该过滤器添加到PushHandlerBroadcastReceiverBase
实现中。我尝试过,但它似乎忽略了这种类型的广播接收器。。。我正在努力找出原因…我昨天一整天都在处理通知,我尝试重新启动设备,但在使用intent筛选器时无法重现此问题。所以你一定是做错了什么,或者你让一个任务经理在杀东西。我会再给它一次机会,看看会发生什么。我试过了,但它不起作用:(我很沮丧。我只是没有得到完整的引导。)。
[Service] //Must use the service tag
public class PushHandlerService : PushHandlerServiceBase
{
public PushHandlerService () : base (PushHandlerBroadcastReceiver.SENDER_IDS)
{
}
protected override void OnRegistered (Context context, string registrationId)
{
...
}
protected override void OnUnRegistered (Context context, string registrationId)
{
...
}
protected override void OnMessage (Context context, Intent intent)
{
...
}
protected override bool OnRecoverableError (Context context, string errorId)
{
...
}
protected override void OnError (Context context, string errorId)
{
...
}
void createNotification (string title, string desc, Intent intent)
{
...
}
}
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class MyBootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
MyNotificationService.RunIntentInService(context, intent);
SetResult(Result.Ok, null, null);
}
}