【问题标题】:Xamarin Android Launch Application from Foreground NotificationXamarin Android 从前台通知启动应用程序
【发布时间】:2022-01-06 10:44:07
【问题描述】:

这是我第一次发帖,如有遗漏,请告知。

我正在开发一个一切正常的前台服务。我的问题是如何从前台服务通知启动应用程序?

一个应用程序关闭,前台服务仍在运行。有没有一种方法可以让用户拉下抽屉并点击正在运行的服务并启动应用程序?

我希望有人可以提供帮助。这是我的服务代码。

   private void startForegroundService()
    {
        try
        {
            var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            var intent = new Intent(this, typeof(ProteoForegroundService));
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, PENDING_INTENT_ID, intent, PendingIntentFlags.OneShot);

          if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                createNotificationChannel(notifcationManager);
            }

            var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            notification.SetAutoCancel(false);
            notification.SetOngoing(true);
            notification.SetSmallIcon(Resource.Drawable.Icon);
            notification.SetContentTitle("Proteo Mobile");
            notification.SetContentText("Proteo Mobile is running");
            notification.SetContentIntent(pendingIntent);
            StartForeground(NOTIFICATION_ID, notification.Build());
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "startForegroundService()" } });
        }
    }

    private void createNotificationChannel(NotificationManager notificationMnaManager)
    {
        try
        {
            var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
                NotificationImportance.Low);
            notificationMnaManager.CreateNotificationChannel(channel);
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "createNotificationChannel()" } });
        }

    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        startForegroundService();

        return StartCommandResult.NotSticky;


    }

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:

    notification.SetContentIntent 方法是设置当用户点击正在运行的服务并且 PendingIntent.GetActivity 方法应该获取活动时要执行的操作的具体方法。如:

    private void startForegroundService()
    {
        try
        {
            var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            var intent = new Intent(this, typeof(MainActivity));
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, PENDING_INTENT_ID, intent, PendingIntentFlags.OneShot);
    
          if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                createNotificationChannel(notifcationManager);
            }
    
            var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            notification.SetAutoCancel(false);
            notification.SetOngoing(true);
            notification.SetSmallIcon(Resource.Drawable.Icon);
            notification.SetContentTitle("Proteo Mobile");
            notification.SetContentText("Proteo Mobile is running");
            notification.SetContentIntent(pendingIntent);
            StartForeground(NOTIFICATION_ID, notification.Build());
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "startForegroundService()" } });
        }
    }
    

    【讨论】:

    • 当我运行应用程序然后点击服务,抽屉关闭并且应用程序没有打开。是否存在调用 pendinf 意图的特定位置,因为目前它仅在公共覆盖 StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 中被调用
    • 但是我在我的设备上测试了代码,它工作正常。我只是更改代码'var intent = new Intent(this , typeof(MainActivity))'。当我点击通知时,mainactivity 立即显示。 @SimonProteo
    • 我就是我。这很好用,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多