Firebase 未使用xamarin表单在前台接收FCM通知

Firebase 未使用xamarin表单在前台接收FCM通知,firebase,xamarin,xamarin.forms,xamarin.ios,firebase-cloud-messaging,Firebase,Xamarin,Xamarin.forms,Xamarin.ios,Firebase Cloud Messaging,我一直在Xamarin中使用firebase集成推送通知。我已经在Firebase控制台项目中创建了开发人员配置文件,并生成了P12文件和附件 我正在从云消息发送通知(从FCM控制台项目),通知在应用程序关闭时接收,如果我启动应用程序,则发送通知,通知不接收 这是Firebase初始化 LoadApplication(new App()); if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))

我一直在Xamarin中使用firebase集成推送通知。我已经在Firebase控制台项目中创建了开发人员配置文件,并生成了P12文件和附件

我正在从云消息发送通知(从FCM控制台项目),通知在应用程序关闭时接收,如果我启动应用程序,则发送通知,通知不接收

这是Firebase初始化

     LoadApplication(new App());

            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
                                                                        (granted, error) =>
                                                                        {
                                                                            if (granted)
                                                                                InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
                                                                        });
            }
            else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                        new NSSet());

                UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
            else
            {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }

//FirebasePushNotificationManager.Initialize(launchOptions,true);
            return base.FinishedLaunching(uiApplication, launchOptions);
在我的例子中,第一个条件为true(如果为(UIDevice.CurrentDevice.CheckSystemVersion(10,0)),通知注册成功

每次调试应用程序时都会调用RegisteredForRemoteNotifications方法

当我从云消息发送通知时,DidReceiveMemoteNotification被调用,但如果应用程序关闭(后台)通知出现,则通知不会在前台显示(出现)

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken, FirebaseTokenType.Sandbox);
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);

    }
    // To receive notifications in foregroung on iOS 9 and below.
    // To receive notifications in background in any iOS version
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired 'till the user taps on the notification launching the application.

        // If you disable method swizzling, you'll need to call this method. 
        // This lets FCM track message delivery and analytics, which is performed
        // automatically with method swizzling enabled.
        FirebasePushNotificationManager.DidReceiveMessage(userInfo);
        // Do your magic to handle the notification data
        System.Console.WriteLine(userInfo);

        completionHandler(UIBackgroundFetchResult.NewData);
    }

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    {
        base.ReceivedRemoteNotification(application, userInfo);
    }



    // To receive notifications in foreground on iOS 10 devices.
    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do your magic to handle the notification data
        System.Console.WriteLine(notification.Request.Content.UserInfo);
    }


    public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
    {
        Console.WriteLine(remoteMessage.AppData);
    }
public override void registeredForremotentifications(UIApplication应用程序,NSData deviceToken)
{
FirebasePushNotificationManager.didRegisterRemotionTifications(deviceToken,FirebaseTokenType.Sandbox);
}
远程通知的公共覆盖无效注册失败(UIApplication应用程序,N错误)
{
FirebasePushNotificationManager.RemoteNotificationRegistration失败(错误);
}
//在iOS 9及以下版本上接收foregroung中的通知。
//在任何iOS版本的后台接收通知
public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
{
//如果您在应用程序处于后台时收到通知消息,
//在用户点击启动应用程序的通知之前,不会触发此回调。
//如果禁用方法swizzling,则需要调用此方法。
//这使FCM能够跟踪消息传递和分析,这是执行的
//自动启用方法swizzling。
FirebasePushNotificationManager.DidReceiveMessage(用户信息);
//使用您的魔法来处理通知数据
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
public override void ReceivedRemoteNotification(UIApplication应用程序,NSDictionary用户信息)
{
base.ReceivedRemoteNotification(应用程序、用户信息);
}
//在iOS 10设备的前台接收通知。
[导出(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]
public void WillPresentNotification(未使用通知中心、未通知通知通知、操作完成处理程序)
{
//使用您的魔法来处理通知数据
System.Console.WriteLine(通知、请求、内容、用户信息);
}
public void应用程序ReceivedRemoteMessage(RemoteMessage RemoteMessage)
{
Console.WriteLine(remoteMessage.AppData);
}
有人能提出一些有用的建议吗


提前谢谢

对于运行iOS 9及以下版本的设备,请覆盖AppDelegate的DidReceiveMemoteNotification方法,以处理客户端应用位于前台时收到的通知以及发送到客户端的所有数据消息。消息是键和值的字典:

public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
   // Do your magic to handle the notification data
   System.Console.WriteLine (userInfo);
}
public override void didReceiveMemoteNotification(UIApplication应用程序、NSDictionary用户信息、Action completionHandler)
{
//使用您的魔法来处理通知数据
System.Console.WriteLine(userInfo);
}
对于iOS 10及更高版本的设备,请实现IUNUserNotificationCenterDelegate接口并重写WillPresentNotification方法,以处理客户端应用程序位于前台时收到的通知:

// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
  // Do your magic to handle the notification data
  System.Console.WriteLine(notification.Request.Content.UserInfo);
}


 public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
{
    Console.WriteLine(remoteMessage.AppData);
}
//在iOS 10设备的前台接收通知。
[导出(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]
public void WillPresentNotification(未使用通知中心、未通知通知通知、操作完成处理程序)
{
//使用您的魔法来处理通知数据
System.Console.WriteLine(通知、请求、内容、用户信息);
}
public void应用程序ReceivedRemoteMessage(RemoteMessage RemoteMessage)
{
Console.WriteLine(remoteMessage.AppData);
}

我解决了未在前台收到通知的问题。 我实现了一个类
NotificationDelegate
,然后通过
UNUserNotificationCenterDelegate
之后,在前台ios10中创建处理通知的方法

 public class NotificationDelegate : UNUserNotificationCenterDelegate
   {
    public NotificationDelegate()
    {
    }

    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do something with the notification
        Console.WriteLine("Active Notification: {0}", notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
    }


    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        // Take action based on Action ID
        switch (response.ActionIdentifier)
        {
            case "reply":
                // Do something
                break;
            default:
                // Take action based on identifier
                if (response.IsDefaultAction)
                {
                    // Handle default action...
                }
                else if (response.IsDismissAction)
                {
                    // Handle dismiss action
                }
                break;
        }
        completionHandler();
    }
  }





I am facing this issue(not receive notification in foreground) because in WillPresentNotification method i am sending only UNNotificationPresentationOptions.Alert (completionHandler(UNNotificationPresentationOptions.Alert);) but both alert and sound are required for notification in Foreground and Background in iOS 10
然后在AppDelegate类中创建注册方法

 public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken, FirebaseTokenType.Sandbox);
        } 
在这种情况下,如果使用
调试模式
,最重要的是

FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken, FirebaseTokenType.Sandbox);
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken, FirebaseTokenType.Production);
在“权利”中,请添加以下内容

<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
</dict>
</plist>
在“权利”中,请添加以下内容

<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
</dict>
</plist>

aps环境
生产

这就是推送通知行为,当你的应用程序在后台时,只有设备接收推送通知。hello@KiShOrE当应用程序在前台时,此DidReceiveRemoteNotification方法不调用。hello@hakim我面临新问题,我实现了IUNUserNotificationCenterDelegate接口,然后第一次接收到的通知都是
forground和background
,但第二次调试时,没有接收到的通知都是
forground和background
,您可能想签出iOS本地通知并用代码创建自己的通知@畸形兄弟。我可以在我的xamarin.ios应用程序中接收通知。但破产管理通知从未生效。我的操作系统版本是12.4.1。你能看一下这个问题吗