在收到推送通知时确定应用程序是否在前台

时间:2017-12-11 15:57:03

标签: push-notification xamarin.forms

iOS会在收到推送通知时调用void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo),无论应用程序是在运行中,还是在后台或前台运行。

当它在前台时,我希望应用程序以不同的方式做出响应。

但我如何检测它所处的状态?

3 个答案:

答案 0 :(得分:4)

检查application state

bool isInBackbround =
    UIApplication.SharedApplication.ApplicationState == UIApplicationState.Background

答案 1 :(得分:0)

您可以监视应用程序生命周期事件:

public partial class App : Xamarin.Forms.Application
{
    public static bool IsInForeground { get; set; } = false;

    protected override void OnStart()
    {
        IsInForeground = true;
    }

    protected override void OnSleep()
    {
        IsInForeground = false;
    }

    protected override void OnResume()
    {
        IsInForeground = true;
    }
}

然后在您的任何页面或服务中检查App.IsInForeground

答案 2 :(得分:0)

您可以使用iOS项目中的自定义UNUserNotificationCenter检测何时点击了推送通知,并使用MessagingCenter与Xamarin.Form共享项目对话。

自定义委托人:

在Xamarin.iOS项目中创建文件CustomUNUserNotificationCenterDelegate.cs:

using System;
using UserNotifications;
using Xamarin.Forms;

namespace MyApp.iOS
{
    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {    
        //This will be called when a user tap a notification and the app will be back in foreground
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            if (response.IsDefaultAction)
            {
                // User tapped on notification. Pass the notification body 
               //(maybe its better to make some null check to prevent NRE. I'll skip it in this example
                var response = notification.Request.Content;
                MessagingCenter.Send("MyApp", "pushClicked", response.Body);
            }

            // Complete handling the notification.
            completionHandler();
        }
    }

    //This will be called when the notification is received when the app is in Foreground
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        //Notification received
        //(maybe its better to make some null check to prevent NRE. I'll skip it in this example
        var response = notification.Request.Content;
        MessagingCenter.Send("MyApp", "pushReceived", response.Body);

        //Complete handling the notification.
        completionHandler(UNNotificationPresentationOptions.None);
    }
}

修改您的AppDelegate.cs

添加到您的AppDelegate.cs中以激活我们的自定义委托:

    //Custom delegate per gestire le push in foreground/cliccate da background
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    }

Xamarin.Forms项目

在Xamarin.Forms项目中,您可以订阅消息并执行任意代码:

    //Subscribe to notification tapped when app is closed
    MessagingCenter.Subscribe<string, string> (this, "pushClicked", (sender, args) =>
    {
        //Do something when a push notification is clicked.
        //your notification body will be in the "args" variable
    });

    //Subscrive to notification received when app is in foreground
    MessagingCenter.Subscribe<string, string> (this, "pushReceived", (sender, args) =>
    {
        //Do something when a push notification is clicked.
        // your notification body will be in the "args" variable
    });
  

当您不需要时,请不要忘记取消订阅MessagingCenter   还是页面/对象容器被丢弃