Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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
Xamarin表单:如何在ios中处理通知单击_Ios_Xamarin.forms_Push Notification - Fatal编程技术网

Xamarin表单:如何在ios中处理通知单击

Xamarin表单:如何在ios中处理通知单击,ios,xamarin.forms,push-notification,Ios,Xamarin.forms,Push Notification,我已经在我的xamarin forms ios项目上实现了推送通知。现在我可以在iPhone上接收邮递员推送的通知。但是当点击通知时,如何从通知中读取模型数据并加载PCL App.xaml.cs 在ios设备中单击通知时,我需要打开PCL项目的内容页(消息列表页)。我已经在我的PCL项目App.xaml.cs上实现了这一点。为此,我需要从AppDelegate.cs加载App.xaml.cs,其中包含从通知接收到的模型数据 按照这种方式,我正在从AndroidMainActivity加载App.

我已经在我的xamarin forms ios项目上实现了推送通知。现在我可以在iPhone上接收邮递员推送的通知。但是当点击通知时,如何从通知中读取模型数据并加载PCL App.xaml.cs

在ios设备中单击通知时,我需要打开PCL项目的内容页(消息列表页)。我已经在我的PCL项目App.xaml.cs上实现了这一点。为此,我需要从AppDelegate.cs加载App.xaml.cs,其中包含从通知接收到的模型数据

按照这种方式,我正在从Android
MainActivity
加载
App.xaml.cs

LoadApplication(new App(notificationdata));
在ios
DidReceiveEmotentification
中,在接收后台和前台模式的通知时调用,但在点击通知时不调用

我的通知正文:
webContentList
是我的模型数据

{
 "to" : "dmtfiSvBBM0:APA91bFnHkamMSYgxPuiSfdvKnU8hD_mOqrWijnENNgXVSkSgo1ILH3-uKVCU7Ez2PXXOhtDoobIyKBf5UshVfTmvjSqHgXMRTsqguKCSTjIfGnXrVP-_cNFq2sisshZO-BcfkwKTl-I",
 "collapse_key" : "type_a",
 "notification" : {
      "body" : "This is body",
     "title": "Tech Team",
     "priority":"high",
     "content_available":true
 },
 "data" : {
    "webContentList": [
        {
            "webContentDefinitionId": 818084,
            "pageTitle": "CCD Grade 3-4",
            "pageKwd": "CCD Grade 3-4",
            "pageDesc": "CCD Grade 3-4",
            "siteId": 45,
            "pageCreatedTime": 1555145959428,
            "pageUpdatedDate": 1555927274279,
            "modifier": {
                "userId": 12944,
                "applicationId": 32,
                "username": "robert.downey",
                "email": "robert@master-mail.net",
                "firstName": "Robert",
                "lastName": "Downey"
            },
            "creator": {
                "userId": 12944,
                "applicationId": 32,
                "username": "robert.downey",
                "email": "robert@master-mail.net",
                "firstName": "Robert",
                "lastName": "Downey"
            }
        }
        ]
 },
  "ttl": 3600
}
我的AppDelegate.cs

    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Rg.Plugins.Popup.Popup.Init();
            global::Xamarin.Forms.Forms.Init();
            ImageCircleRenderer.Init();
            LoadApplication(new App("", ""));

            #region Push Notification            
            Firebase.Core.App.Configure();
            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;

                // For iOS 10 data message (sent via FCM)
                //Messaging.SharedInstance.RemoteMessageDelegate = this;
            }
            else
            {
                // iOS 9 or before
                var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
                var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            }

            UIApplication.SharedApplication.RegisterForRemoteNotifications();

            Messaging.SharedInstance.Delegate = this;
            Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
            #endregion

            return base.FinishedLaunching(app, options);
        }

        [Export("messaging:didReceiveRegistrationToken:")]
        public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
        {
            LogInformation(nameof(DidReceiveRegistrationToken), $"Firebase registration token: {fcmToken}"
            MessagingCenter.Send<object, string>(this, "fcmtoken", fcmToken.ToString());
            Console.WriteLine($"fcmtoken received: {fcmToken}");
        }

        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
            HandleMessage(userInfo);
            // Print full message.
            LogInformation(nameof(DidReceiveRemoteNotification), userInfo);
            completionHandler(UIBackgroundFetchResult.NewData);
        }

        [Export("messaging:didReceiveMessage:")]
        public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
        {
            // Handle Data messages for iOS 10 and above.
            HandleMessage(remoteMessage.AppData);
            LogInformation(nameof(DidReceiveMessage), remoteMessage.AppData);
        }

        void HandleMessage(NSDictionary message)
        {
            //if (MessageReceived == null)
            //    return;

            //MessageType messageType;
            //if (message.ContainsKey(new NSString("aps")))
            //    messageType = MessageType.Notification;
            //else
            //    messageType = MessageType.Data;

            //var e = new UserInfoEventArgs(message, messageType);
            //MessageReceived(this, e);
        }

        public static void ShowMessage(string title, string message, UIViewController fromViewController, Action actionForOk = null)
        {
            var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (obj) => actionForOk?.Invoke()));
            fromViewController.PresentViewController(alert, true, null);
        }

        void LogInformation(string methodName, object information) => Console.WriteLine($"\nMethod name: {methodName}\nInformation: {information}");

        async Task RequestPushPermissionAsync()
        {
            // iOS10 and later (https://developer.xamarin.com/guides/ios/platform_features/user-notifications/enhanced-user-notifications/#Preparing_for_Notification_Delivery)
            // Register for ANY type of notification (local or remote):
            var requestResult = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(
                UNAuthorizationOptions.Alert
                | UNAuthorizationOptions.Badge
                | UNAuthorizationOptions.Sound);


            // Item1 = approved boolean
            bool approved = requestResult.Item1;
            NSError error = requestResult.Item2;
            if (error == null)
            {
                // Handle approval
                if (!approved)
                {
                    Console.Write("Permission to receive notifications was not granted.");
                    return;
                }

                var currentSettings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync();
                if (currentSettings.AuthorizationStatus != UNAuthorizationStatus.Authorized)
                {
                    Console.WriteLine("Permissions were requested in the past but have been revoked (-> Settings app).");
                    return;
                }

                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
            else
            {
                Console.Write($"Error requesting permissions: {error}.");
            }
        }
    }
}

webContentList
是我的模型数据,我在加载App.xaml.cs时需要这些数据。如何解析来自DidReceiveRemoteNotification用户信息的数据并加载App.xaml.cs?

使用
Newtonsoft.Json
对Json进行反序列化,并使用
消息中心
传递数据

AppDelegate.cs
public override void didReceiveMemoteNotification(UIApplication应用程序、NSDictionary用户信息、Action completionHandler)
{
HandleMessage(用户信息);
//打印完整消息。
登录信息(姓名(DidReceiveMemoteNotification)、用户信息);
completionHandler(UIBackgroundFetchResult.NewData);
var myData=JsonConvert.DeserializeObject(userInfo[new NSString(“webContentList”)]作为NSString);
Send(这是“PassData”,myData);
}
App.cs
MessagingCenter.Subscribe(这个“PassData”,(对象对象对象,列表)=>{
});
Method name: DidReceiveRemoteNotification
Information: {
    aps =     {
        alert =         {
            body = "This is body";
            title = "Tech Team";
        };
        "content-available" = 1;
    };
    "gcm.message_id" = 1562648547588794;
    "gcm.notification.priority" = high;
    "google.c.a.e" = 1;
    webContentList = "[{\"swcmMessage\":null,\"pageTitle\":\"CCD Grade 3-4\",\"modifier\":{\"lastname\":\"downey\",\"zipcode\":null,\"parentemail\":null,\"address2\":null,\"city\":null,\"address1\":null,\"phone2\":null,\"userid\":12944,\"enabled\":true,\"phone1\":null,\"firstname\":\"robert\",\"state\":null,\"usertype\":null,\"applicationid\":32,\"profileimageurl\":null,\"profileimagetype\":null,\"email\":\"robert@master-mail.net\",\"username\":\"robert.downey\"},\"deletable\":true,\"pagecontenttype\":\"tweets.topics\",\"previewuuid\":null,\"webcontentdefinitionid\":818084,\"pagecreatedtime\":1555145959428,\"usercreated\":12944,\"pagestatus\":\"on\",\"thumbnailimageurl\":null,\"videourl\":null,\"imageurl\":\"\",\"webcontentid\":65059,\"creator\":{\"lastname\":\"downey\",\"zipcode\":null,\"parentemail\":null,\"address2\":null,\"city\":null,\"address1\":null,\"phone2\":null,\"userid\":12944,\"enabled\":true,\"phone1\":null,\"firstname\":\"robert\",\"state\":null,\"usertype\":null,\"applicationid\":32,\"profileimageurl\":null,\"profileimagetype\":null,\"email\":\"robert\"},\"contentTemplateId\":null,\"appName\":\"services\",\"customHTML\":\"\",\"processedTime\":null,\"pageDesc\":\"CCD Grade 3-4\",\"editUrl\":\"\\/module\\/tweets-topics\\/edit-tweets-topics?webcontentid=818084\",\"pageKwd\":\"CCD Grade 3-4\",\"staticContent\":true,\"siteId\":45,\"pageUrl\":\"\\/tweets-topics\\/818084\\/1\\/ccd-grade-3-4\",\"linkType\":null,\"pageUpdatedDate\":1555927274279,\"swcmStatus\":null,\"userModified\":12944}]";
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    HandleMessage(userInfo);
    // Print full message.
    LogInformation(nameof(DidReceiveRemoteNotification), userInfo);        
    completionHandler(UIBackgroundFetchResult.NewData);

    var myData = JsonConvert.DeserializeObject<List<YourModel>>(userInfo[new NSString("webContentList")] as NSString);
    MessagingCenter.Send<object,List<YourModel>>(this,"PassData",myData);
}
MessagingCenter.Subscribe<object, List<YourModel>>(this, "PassData", (object obj , List<YourModel> list)=>{

});