在xamarin android中几秒后自动清除GCM推送通知

时间:2017-10-23 13:58:49

标签: xamarin xamarin.forms xamarin.android

我正在尝试在应用程序无法在后台运行时获取推送通知。我尝试了很多解决方案但没有正确收到通知。当应用程序没有在后台运行时,我尝试使用解决方案来获取推送通知。它在较低的Android设备上接收推送通知,并且它在通知栏中仅显示几秒钟。然后它自动清除。当应用程序没有在后台运行时获取通知的问题是什么。自动清除通知的原因是什么。请提出任何想法。

示例代码:

BroadcastReceiver:

  [assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
  [assembly: UsesPermission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
  [assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]

  //GET_ACCOUNTS is needed only for Android versions 4.0.3 and below
  [assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]
  [assembly: UsesPermission(Name = "android.permission.INTERNET")]
  [assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
  namespace SampleApp.Droid.PushNotificationHelper
  {
   [BroadcastReceiver(Permission = Gcm.Client.Constants.PERMISSION_GCM_INTENTS)]
   [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_MESSAGE },
   Categories = new string[] { "@PACKAGE_NAME@" })]
   [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
   Categories = new string[] { "@PACKAGE_NAME@" })]
   [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_LIBRARY_RETRY },
   Categories = new string[] { "@PACKAGE_NAME@" })]
[IntentFilter(new[] { Intent.ActionBootCompleted })]

public class BroadcastReceiver : GcmBroadcastReceiverBase<PushHandlerService>
{
    public static string[] SENDER_IDS = new string[] { Constants.PushNotificationSenderId };

    public const string TAG = "MyBroadcastReceiver-GCM";

    public override void OnReceive(Context context, Intent intent)
    {
        base.OnReceive(context, intent);

        var pm = PowerManager.FromContext(context);
        var wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Reciever Tag");
        wakeLock.Acquire();

        RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
        ActivityManager.GetMyMemoryState(myProcess);
        var isInForeground = myProcess.Importance != Importance.Foreground;

         if (isInForeground)
        {
            Intent serviceIntent = new Intent(context, typeof(PushHandlerService));
            context.StartService(serviceIntent);
        }
    }
}

0 个答案:

没有答案