Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DidReceiveRemoteNotification从未在Xamarin.iOS中调用_Ios_Firebase_Push Notification_Xamarin.ios_Firebase Cloud Messaging - Fatal编程技术网

DidReceiveRemoteNotification从未在Xamarin.iOS中调用

DidReceiveRemoteNotification从未在Xamarin.iOS中调用,ios,firebase,push-notification,xamarin.ios,firebase-cloud-messaging,Ios,Firebase,Push Notification,Xamarin.ios,Firebase Cloud Messaging,我正在尝试使用Xamarin和Xamarin.Firebase.iOS.CloudMessaging包为我的iOS应用程序实现推送通知 我已经把一切都准备好了。当应用程序位于前台时,我可以接收通知(包含和不包含“可用内容”标记),并与之交互(点击通知等) 但是,当应用程序在后台时,我会收到通知,但不会调用回调。据我所知,在以下情况下应调用“DidReceiveMemotentification”: 无论是否启用了“内容可用”标记,应用程序都位于前台 如果应用程序处于后台或挂起状态,则启用“内容可

我正在尝试使用Xamarin和Xamarin.Firebase.iOS.CloudMessaging包为我的iOS应用程序实现推送通知

我已经把一切都准备好了。当应用程序位于前台时,我可以接收通知(包含和不包含“
可用内容”
标记),并与之交互(点击通知等)

但是,当应用程序在后台时,我会收到通知,但不会调用回调。据我所知,在以下情况下应调用
“DidReceiveMemotentification”

  • 无论是否启用了
    “内容可用”
    标记,应用程序都位于前台
  • 如果应用程序处于后台或挂起状态,则启用
    “内容可用”
    标记
  • 当用户点击通知时
  • 以下是我在AppDelegate.cs中实现的功能:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        Firebase.Core.App.Configure();
        FireBaseRegistration();
        return base.FinishedLaunching(app, options);
    }
    
    
    private void FireBaseRegistration()
    {
        // Register your app for remote notifications.
        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            // iOS 10 or later
            var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
            UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
                Console.WriteLine(granted);
            });
    
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = this;
        }
        else
        {  
            // iOS 9 or before
            var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
            var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
        }
    
        //Register for APNs notifications
        UIApplication.SharedApplication.RegisterForRemoteNotifications();
    
        Messaging.SharedInstance.Delegate = this;
    
        var token = InstanceId.SharedInstance.Token;
        Debug.WriteLine(token);
    
        //////Connect to FCM (Only used for Foreground notifications)
        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
    
        // Monitor token generation
        InstanceId.Notifications.ObserveTokenRefresh((sender, e) => {
            // Note that this callback will be fired everytime a new token is generated, including the first
            // time. So if you need to retrieve the token as soon as it is available this is where that
            // should be done.
            token = InstanceId.SharedInstance.Token;
            Console.WriteLine(token);
    
        });
    
    }
    
    
    //Register APNs token because method swizzling is de-activated
    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        Console.WriteLine("Registered for remote notifications");
        Console.WriteLine(deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None));
        Console.WriteLine(deviceToken);
    }
    
    
    
    [Export("messaging:didReceiveMessage:")]
    public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
    {
        // Do your magic to handle the notification data
        Console.WriteLine("iOS 11 Foreground");
    }
    
    
    //Shows local notification and is called when user taps notification
    [Export("userNotificationCenter:DidReceiveRemoteNotification:withCompletionHandler:")]
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        Console.WriteLine("Received a notficaiton");
        completionHandler(UIBackgroundFetchResult.NewData);
    }
    
    //To receive notifications in foreground on iOS 11 devices.
    [Export("userNotificationCenter:willPresent:withCompletionHandler:")]
    public void WillPresent(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling iOS 11 foreground notification");
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }
    
    ////Called when tapping notification
    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        Console.WriteLine("Handling push notificaiton interaction");
        completionHandler();
    }
    
    //Receive data message on iOS 10 devices.
    public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
    {
        Console.WriteLine("Handling iOS 10 data message notification");
    }   
    
    //// To receive notifications in foreground on iOS 10 devices.
    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling foreground notification");
        completionHandler(UNNotificationPresentationOptions.Alert);
    }
    
    public override bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
    {
    全局::Xamarin.Forms.Forms.Init();
    Firebase.Core.App.Configure();
    FireBaseRegistration();
    返回基地。完成发射(应用程序,选项);
    }
    私有无效FireBaseRegistration()
    {
    //为远程通知注册你的应用程序。
    if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
    {
    //iOS 10或更高版本
    var authOptions=未授权选项。警报|未授权选项。徽章|未授权选项。声音;
    UnuseNotificationCenter.Current.RequestAuthorization(authOptions,(已授予,错误)=>{
    控制台写入线(已授予);
    });
    //对于iOS 10显示通知(通过APNS发送)
    UNUserNotificationCenter.Current.Delegate=此;
    }
    其他的
    {  
    //iOS 9或之前
    var allNotificationTypes=UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    var settings=UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes,null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings(设置);
    }
    //注册APNs通知
    UIApplication.SharedApplication.RegisterForRemotonifications();
    Messaging.SharedInstance.Delegate=此;
    var token=InstanceId.SharedInstance.token;
    Debug.WriteLine(令牌);
    //////连接到FCM(仅用于前台通知)
    Messaging.SharedInstance.ShouldStablishDirectChannel=true;
    //监视令牌生成
    InstanceId.Notifications.ObserveTokenRefresh((发送方,e)=>{
    //请注意,每次生成新令牌(包括第一个令牌)时,都会触发此回调
    //时间。因此,如果您需要在令牌可用时立即检索令牌,这就是
    //应该这样做。
    token=InstanceId.SharedInstance.token;
    控制台写入线(令牌);
    });
    }
    //注册APNs令牌,因为方法swizzling已取消激活
    公共覆盖无效注册更正(UIApplication应用程序,NSData deviceToken)
    {
    Console.WriteLine(“为远程通知注册”);
    WriteLine(deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None));
    控制台写入线(deviceToken);
    }
    [导出(“消息传递:didReceiveMessage:”)]
    public void DidReceiveMessage(消息传递、远程消息、远程消息)
    {
    //使用您的魔法来处理通知数据
    控制台写入线(“iOS 11前台”);
    }
    //显示本地通知,并在用户点击通知时调用
    [导出(“userNotificationCenter:DidReceiveMemoteNotification:withCompletionHandler:”)]
    public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
    {
    Console.WriteLine(“收到通知”);
    completionHandler(UIBackgroundFetchResult.NewData);
    }
    //在iOS 11设备的前台接收通知。
    [导出(“userNotificationCenter:willPresent:withCompletionHandler:”)]
    public void将出现(UnuseNotificationCenter、UNNotification通知、Action completionHandler)
    {
    Console.WriteLine(“处理iOS 11前台通知”);
    completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }
    ////点击通知时调用
    [导出(“userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:”)]
    public void direceiveNotificationResponse(未使用通知中心、未通知响应、操作完成处理程序)
    {
    Console.WriteLine(“处理推送通知交互”);
    completionHandler();
    }
    //在iOS 10设备上接收数据消息。
    public void应用程序ReceivedRemoteMessage(RemoteMessage RemoteMessage)
    {
    Console.WriteLine(“处理iOS 10数据消息通知”);
    }   
    ////在iOS 10设备的前台接收通知。
    [导出(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]
    public void WillPresentNotification(未使用通知中心、未通知通知通知、操作完成处理程序)
    {
    Console.WriteLine(“处理前台通知”);
    completionHandler(UNNotificationPresentationOptions.Alert);
    }
    
    我已经在iOS 10.3.3和iOS 11.2上试用过了。以下是两个版本所调用的函数:

  • 当应用程序位于前台时
    • 在iOS 10.3.3上:
      WillPresentNotification()
      (带或不带
      “内容可用”
      标记)
    • 在iOS 11.2上:
      WillPresentNotification()
      (带或不带
      “内容可用”
      标记)
  • 当应用程序处于后台时:
    • 在iOS 10.3.3上:没有什么不应该的,不是吗
    • 在iOS 11.2上:没有什么不应该的,不是吗
  • 我有