Ejemplo n.º 1
0
        public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            // The deviceToken is what the push notification server needs to send out a notification
            // to the device. Most times application needs to send the device Token to its servers when it has changed

                        #if DEBUG
            log("Success registering for Remote Notifications");
                        #endif
            // ****** REMOVED "lastDeviceToken storage" feature. Marga 06/08/2013 . Platform will always call the JS listener; same behavior in all platforms ******

            // First, get the last device token we know of
            // string lastDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("deviceToken");

            //There's probably a better way to do this
            //NSString strFormat = new NSString("%@");
            //NSString newToken = new NSString(ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr(new ObjCRuntime.Class("NSString").Handle, new ObjCRuntime.Selector("stringWithFormat:").Handle, strFormat.Handle, deviceToken.Handle));

            NSString newToken = new NSString(deviceToken.ToString());

            var newDeviceToken = newToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
                        #if DEBUG
            log("Device token: " + newDeviceToken);
                        #endif
            // We only want to send the device token to the server if it hasn't changed since last time
            // no need to incur extra bandwidth by sending the device token every time
            // if (!newDeviceToken.Equals(lastDeviceToken))
            //{
            // Send the new device token to your application server
            // ****** REMOVED "lastDeviceToken storage" feature. Marga 06/08/2013 . Platform will always call the JS listener; same behavior in all platforms ******

            RegistrationToken registrationToken = new RegistrationToken();
            registrationToken.StringRepresentation = newDeviceToken;
            byte[] buffer = new byte[deviceToken.Length];
            Marshal.Copy(deviceToken.Bytes, buffer, 0, buffer.Length);
            registrationToken.Binary = buffer;
            PushNotificationsUtils.FireUnityJavascriptEvent("Appverse.PushNotifications.OnRegisterForRemoteNotificationsSuccess", registrationToken);

            //Save the new device token for next application launch
            // NSUserDefaults.StandardUserDefaults.SetString(newDeviceToken, "deviceToken");
        }
        public async override Task RegisterForRemoteNotifications(string senderId, RemoteNotificationType[] types)
        {
            try
            {

                _pushNotificationChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

                _pushNotificationChannel.PushNotificationReceived += OnPushNotificationReceived;

                var registrationToken = new RegistrationToken
                {
                    Binary = Encoding.UTF8.GetBytes(_pushNotificationChannel.Uri),
                    StringRepresentation = _pushNotificationChannel.Uri
                };
                WindowsPhoneUtils.InvokeCallback("Appverse.PushNotifications.OnRegisterForRemoteNotificationsSuccess", WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(registrationToken));
            }
            catch (Exception ex)
            {

                var registrationError = new RegistrationError
                {
                    Code = ex.HResult.ToString(),
                    LocalizedDescription = ex.Message
                };
                WindowsPhoneUtils.InvokeCallback("Appverse.PushNotifications.OnRegisterForRemoteNotificationsFailure", WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(registrationError));
            }
        }
        public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            // The deviceToken is what the push notification server needs to send out a notification
            // to the device. Most times application needs to send the device Token to its servers when it has changed

            #if DEBUG
            log ("Success registering for Remote Notifications");
            #endif
            // ****** REMOVED "lastDeviceToken storage" feature. Marga 06/08/2013 . Platform will always call the JS listener; same behavior in all platforms ******

            // First, get the last device token we know of
            // string lastDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("deviceToken");

            //There's probably a better way to do this
            //NSString strFormat = new NSString("%@");
            //NSString newToken = new NSString(ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr(new ObjCRuntime.Class("NSString").Handle, new ObjCRuntime.Selector("stringWithFormat:").Handle, strFormat.Handle, deviceToken.Handle));

            NSString newToken = new NSString (deviceToken.ToString ());

            var newDeviceToken = newToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
            #if DEBUG
            log ("Device token: " + newDeviceToken);
            #endif
            // We only want to send the device token to the server if it hasn't changed since last time
            // no need to incur extra bandwidth by sending the device token every time
            // if (!newDeviceToken.Equals(lastDeviceToken))
            //{
            // Send the new device token to your application server
            // ****** REMOVED "lastDeviceToken storage" feature. Marga 06/08/2013 . Platform will always call the JS listener; same behavior in all platforms ******

            RegistrationToken registrationToken = new RegistrationToken();
            registrationToken.StringRepresentation = newDeviceToken;
            byte[] buffer = new byte[deviceToken.Length];
            Marshal.Copy(deviceToken.Bytes, buffer,0,buffer.Length);
            registrationToken.Binary = buffer;
            PushNotificationsUtils.FireUnityJavascriptEvent("Appverse.PushNotifications.OnRegisterForRemoteNotificationsSuccess", registrationToken);

            //Save the new device token for next application launch
            // NSUserDefaults.StandardUserDefaults.SetString(newDeviceToken, "deviceToken");
        }