【问题标题】:Android not receiving PushNotification when App is "closed"当应用程序“关闭”时,Android 未收到 PushNotification
【发布时间】:2018-01-18 12:51:27
【问题描述】:

我正在使用 GCM 服务进行推送通知,当应用程序打开或处于后台时它工作正常,但当应用程序关闭时它会崩溃:

例外:

致命异常:主进程:br.com.siagri.SiagriAutorize,PID: 15786 android.runtime.JavaProxyThrowable: System.NotSupportedException:无法激活 JNI 句柄 0xbeed8498 (key_handle 0xa7376b4) Java 类型 'md5d8b390bb35897e87a51d1b4384dffa30/GcmService' 作为托管类型 'SiagriAuth.Droid.Services.GcmService'。 ---> System.InvalidOperationException:您必须调用 Xamarin.Forms.Init(); 在使用之前。

我关注了这个tutorial 并查看了这个sample 但是当应用程序关闭时,我没有看到任何关于/不同的通知工作。

这是我接收和处理通知的方法:

 private void CreateNotification(string title, string desc, string approvalCode)
        {
            try
            {
                //Create notification
                var notificationManager = GetSystemService(NotificationService) as NotificationManager;

                //Create an intent to show ui
                var uiIntent = new Intent(this, typeof(MainActivity));

                //Use Notification Builder
                var builder = new NotificationCompat.Builder(this);

                //Create the notification
                //we use the pending intent, passing our ui intent over which will get called
                //when the notification is tapped.
                var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetTicker(title)
                        .SetContentTitle(title)
                        .SetContentText(desc)
                        .SetPriority((int)NotificationPriority.High)
                        .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
                        //Set the notification sound
                        .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                        //Auto cancel will remove the notification once the user touches it
                        .SetAutoCancel(true).Build();
                //Show the notification

                int uniqueIdentifier = 0;
                for (int i = 0; i < approvalCode.Length; i++)
                {
                    uniqueIdentifier += (int)approvalCode[i];
                }
                notificationManager?.Notify(uniqueIdentifier, notification);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                App.LocalAlert("Erro", "Erro ao criar notificação.");
            }
        }

        protected override void OnMessage(Context context, Intent intent)
        {
            try
            {
                if (intent != null && intent.Extras != null)
                {
                    var messageText = intent.Extras.GetString("message");
                    var approvalCode = intent.Extras.GetString("ApprovalCode");
                    if (!string.IsNullOrEmpty(messageText) && !string.IsNullOrEmpty(approvalCode))
                        CreateNotification($"Solicitação - {approvalCode}", messageText, approvalCode);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                App.LocalAlert("Erro", "Erro ao criar notificação.");
            }
        }

我正在使用这些程序集:

[assembly: Permission(Name = "myProject.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "myProject.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]
[assembly: UsesPermission(Name = "android.permission.INTERNET")]
[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]

还有这些 IntentFilter:

 [BroadcastReceiver(Permission = Constants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_MESSAGE }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]

你知道如何让它适用于封闭的应用吗?

Obs.:我已经在发布模式下运行应用程序。

【问题讨论】:

  • i'm gotting an error message 错误信息是什么?
  • 嗨@SushiHangover 很抱歉说我收到一条错误消息,应用程序崩溃了。我提供了一张图片。
  • 查看logcat了解崩溃的详细信息,它是在本地通知创建期间还是在用户点击通知时崩溃?
  • 我不知道它在哪里崩溃导致它关闭所以我无法调试,当我在调试模式下运行时,后台和打开,工作正常。 @SushiHangover
  • Visual Studio->tools->Android->Android Device Monitors,选择你的手机,你会看到登录控制台。

标签: android azure xamarin google-cloud-messaging azure-notificationhub


【解决方案1】:

我的错误是在 GcmService 的构造中,我正在那里实例化我的存储库,所以我试图访问数据库,我只是从构造函数中删除并放入我注册的方法,这就是我使用它的地方。

 public GcmService() : base(PushHandlerBroadcastReceiver.SenderIds)
    {
        Log.Info(PushHandlerBroadcastReceiver.Tag, "PushHandlerService() constructor");
        _permissaoRepository = new PermissaoRepository(App.SiagriAuthContext);
        _loginRepository = new LoginRepository(App.SiagriAuthContext);
    }

我改成:

public GcmService() : base(PushHandlerBroadcastReceiver.SenderIds)
    {
        Log.Info(PushHandlerBroadcastReceiver.Tag, "PushHandlerService() constructor");
    }

所以当应用程序关闭时我无法访问我的数据库,这是非常合理的。 我快速搜索了在应用程序关闭时尝试使用数据库但我没有找到,但herehere 有一些关于使用本地数据库的信息。

【讨论】:

  • 为我节省了很多时间。谢谢!
猜你喜欢
  • 2018-04-01
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2013-09-28
  • 2021-06-10
  • 1970-01-01
相关资源
最近更新 更多